| 1 | """ |
|---|
| 2 | Ported to Python 3. |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | from urllib.parse import quote as url_quote |
|---|
| 6 | from allmydata.scripts.common_http import do_http, check_http_error |
|---|
| 7 | from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, UnknownAliasError |
|---|
| 8 | from allmydata.util.encodingutil import quote_output |
|---|
| 9 | |
|---|
| 10 | def mkdir(options): |
|---|
| 11 | nodeurl = options['node-url'] |
|---|
| 12 | aliases = options.aliases |
|---|
| 13 | where = options.where |
|---|
| 14 | stdout = options.stdout |
|---|
| 15 | stderr = options.stderr |
|---|
| 16 | if not nodeurl.endswith("/"): |
|---|
| 17 | nodeurl += "/" |
|---|
| 18 | if where: |
|---|
| 19 | try: |
|---|
| 20 | rootcap, path = get_alias(aliases, where, DEFAULT_ALIAS) |
|---|
| 21 | except UnknownAliasError as e: |
|---|
| 22 | e.display(stderr) |
|---|
| 23 | return 1 |
|---|
| 24 | |
|---|
| 25 | if not where or not path: |
|---|
| 26 | # create a new unlinked directory |
|---|
| 27 | url = nodeurl + "uri?t=mkdir" |
|---|
| 28 | if options["format"]: |
|---|
| 29 | url += "&format=%s" % url_quote(options['format']) |
|---|
| 30 | resp = do_http("POST", url) |
|---|
| 31 | rc = check_http_error(resp, stderr) |
|---|
| 32 | if rc: |
|---|
| 33 | return rc |
|---|
| 34 | new_uri = resp.read().strip() |
|---|
| 35 | # emit its write-cap |
|---|
| 36 | print(quote_output(new_uri, quotemarks=False), file=stdout) |
|---|
| 37 | return 0 |
|---|
| 38 | |
|---|
| 39 | # create a new directory at the given location |
|---|
| 40 | path = str(path, "utf-8") |
|---|
| 41 | if path.endswith("/"): |
|---|
| 42 | path = path[:-1] |
|---|
| 43 | # path must be "/".join([s.encode("utf-8") for s in segments]) |
|---|
| 44 | url = nodeurl + "uri/%s/%s?t=mkdir" % (url_quote(rootcap), |
|---|
| 45 | url_quote(path)) |
|---|
| 46 | if options['format']: |
|---|
| 47 | url += "&format=%s" % url_quote(options['format']) |
|---|
| 48 | |
|---|
| 49 | resp = do_http("POST", url) |
|---|
| 50 | check_http_error(resp, stderr) |
|---|
| 51 | new_uri = resp.read().strip() |
|---|
| 52 | print(quote_output(new_uri, quotemarks=False), file=stdout) |
|---|
| 53 | return 0 |
|---|