addurl: Add --pathdepth option.

This commit is contained in:
Joey Hess 2012-02-16 12:25:19 -04:00
parent a86d937b5b
commit 39c3f56b33
3 changed files with 36 additions and 15 deletions

View file

@ -22,34 +22,40 @@ import qualified Option
import Types.Key import Types.Key
def :: [Command] def :: [Command]
def = [withOptions [fileOption] $ def = [withOptions [fileOption, pathdepthOption] $
command "addurl" (paramRepeating paramUrl) seek "add urls to annex"] command "addurl" (paramRepeating paramUrl) seek "add urls to annex"]
fileOption :: Option fileOption :: Option
fileOption = Option.field [] "file" paramFile "specify what file the url is added to" fileOption = Option.field [] "file" paramFile "specify what file the url is added to"
pathdepthOption :: Option
pathdepthOption = Option.field [] "pathdepth" paramFile "number of path components to use in filename"
seek :: [CommandSeek] seek :: [CommandSeek]
seek = [withField fileOption return $ \f -> seek = [withField fileOption return $ \f ->
withStrings $ start f] withField pathdepthOption (return . maybe Nothing readish) $ \d ->
withStrings $ start f d]
start :: Maybe FilePath -> String -> CommandStart start :: Maybe FilePath -> Maybe Int -> String -> CommandStart
start optfile s = notBareRepo $ go $ fromMaybe bad $ parseURI s start optfile pathdepth s = notBareRepo $ go $ fromMaybe bad $ parseURI s
where where
bad = fromMaybe (error $ "bad url " ++ s) $ bad = fromMaybe (error $ "bad url " ++ s) $
parseURI $ escapeURIString isUnescapedInURI s parseURI $ escapeURIString isUnescapedInURI s
go url = do go url = do
let file = fromMaybe (url2file url) optfile let file = fromMaybe (url2file url pathdepth) optfile
showStart "addurl" file showStart "addurl" file
next $ perform s file next $ perform s file pathdepth
perform :: String -> FilePath -> CommandPerform perform :: String -> FilePath -> Maybe Int -> CommandPerform
perform url file = ifAnnexed file addurl geturl perform url file pathdepth = ifAnnexed file addurl geturl
where where
geturl = do geturl = do
liftIO $ createDirectoryIfMissing True (parentDir file) liftIO $ createDirectoryIfMissing True (parentDir file)
fast <- Annex.getState Annex.fast fast <- Annex.getState Annex.fast
if fast then nodownload url file else download url file if fast then nodownload url file else download url file
addurl (key, _backend) = do addurl (key, _backend) = do
when (pathdepth /= Nothing) $
error $ file ++ " already exists"
unlessM (liftIO $ Url.check url (keySize key)) $ unlessM (liftIO $ Url.check url (keySize key)) $
error $ "failed to verify url: " ++ url error $ "failed to verify url: " ++ url
setUrlPresent key url setUrlPresent key url
@ -80,8 +86,17 @@ nodownload url file = do
setUrlPresent key url setUrlPresent key url
next $ Command.Add.cleanup file key False next $ Command.Add.cleanup file key False
url2file :: URI -> FilePath url2file :: URI -> Maybe Int -> FilePath
url2file url = take 255 $ escape $ uriRegName auth ++ uriPath url ++ uriQuery url url2file url pathdepth = case pathdepth of
Nothing -> filesize $ escape fullurl
Just depth
| depth > 0 -> filesize $ join "/" $
fromend depth $ map escape $
filter (not . null) $ split "/" fullurl
| otherwise -> error "bad --pathdepth value"
where where
escape = replace "/" "_" . replace "?" "_" fullurl = uriRegName auth ++ uriPath url ++ uriQuery url
auth = fromMaybe (error $ "bad url " ++ show url) $ uriAuthority url auth = fromMaybe (error $ "bad url " ++ show url) $ uriAuthority url
filesize = take 255
escape = replace "/" "_" . replace "?" "_"
fromend n = reverse . take n . reverse

1
debian/changelog vendored
View file

@ -29,6 +29,7 @@ git-annex (3.20120124) UNRELEASED; urgency=low
* whereis: Prints the urls of files that the web special remote knows about. * whereis: Prints the urls of files that the web special remote knows about.
* Added a annex.queuesize setting, useful when adding hundreds of thousands * Added a annex.queuesize setting, useful when adding hundreds of thousands
of files on a system with plenty of memory. of files on a system with plenty of memory.
* addurl: Add --pathdepth option.
-- Joey Hess <joeyh@debian.org> Tue, 24 Jan 2012 16:21:55 -0400 -- Joey Hess <joeyh@debian.org> Tue, 24 Jan 2012 16:21:55 -0400

View file

@ -149,10 +149,15 @@ subdirectories).
To avoid immediately downloading the url, specify --fast. To avoid immediately downloading the url, specify --fast.
To specify what file the url is added to, specify --file. This changes Normally the filename is based on the full url, so will look like
the behavior; now all the specified urls are recorded as alternate "www.example.com_subdir_bigfile". For a shorter filename, specify
locations from which the file can be downloaded. In this mode, addurl --pathdepth=N. For example, --pathdepth=2 will use "subdir/bigfile",
can be used both to add new files, or to add urls to existing files. while --parhdepth=1 will use "bigfile".
Or, to directly specify what file the url is added to, specify --file.
This changes the behavior; now all the specified urls are recorded as
alternate locations from which the file can be downloaded. In this mode,
addurl can be used both to add new files, or to add urls to existing files.
# REPOSITORY SETUP COMMANDS # REPOSITORY SETUP COMMANDS