fedora-img-dl 0.2 → 0.3
raw patch · 4 files changed
+76/−44 lines, 4 files
Files
- CHANGELOG.md +4/−0
- Main.hs +69/−39
- README.md +2/−4
- fedora-img-dl.cabal +1/−1
CHANGELOG.md view
@@ -1,5 +1,9 @@ # Changelog +## 0.3+- run sha256sum check+- support fedora Spins+ ## 0.2 - fix and improve symlink naming - use new http-directory library to check exact filesize
Main.hs view
@@ -7,7 +7,7 @@ import Control.Monad (when, unless) import qualified Data.ByteString.Char8 as B-import Data.Char (isDigit, toLower)+import Data.Char (isDigit, toLower, toUpper) import Data.List (intercalate) import Data.Maybe import Data.Semigroup ((<>))@@ -40,13 +40,21 @@ import qualified Text.ParserCombinators.ReadP as R import qualified Text.ParserCombinators.ReadPrec as RP +{-# ANN module "HLint: ignore Use camelCase" #-} data FedoraEdition = Cloud | Container | Everything | Server | Silverblue | Workstation- deriving (Show, Enum, Bounded)+ | Cinnamon+ | KDE+ | LXDE+ | LXQt+ | Mate_Compiz+ | Soas+ | Xfce+ deriving (Show, Enum, Bounded, Eq) instance Read FedoraEdition where readPrec = do@@ -59,16 +67,20 @@ Nothing -> error' "unknown edition" >> RP.pfail Just ed -> RP.lift (R.string e) >> return ed +fedoraSpins :: [FedoraEdition]+fedoraSpins = [Cinnamon ..]+ main :: IO () main = let pdoc = Just $ P.text "Tool for downloading Fedora iso file images."- P.<$$> P.text "RELEASE can be 'rawhide', 'respin', 'beta' or release version" in+ P.<$$> P.text ("RELEASE = " <> intercalate ", " ["rawhide", "respin", "beta", "or Release version"])+ P.<$$> P.text "EDITION = " <> P.lbrace <> P.align (P.fillCat (P.punctuate P.comma (map (P.text . map toLower . show) [(minBound :: FedoraEdition)..maxBound])) <> P.rbrace) in simpleCmdArgsWithMods (Just version) (fullDesc <> header "Fedora iso downloader" <> progDescDoc pdoc) $ findISO <$> optional (strOptionWith 'm' "mirror" "HOST" "default https://download.fedoraproject.org") <*> switchWith 'n' "dry-run" "Don't actually download anything" <*> strOptionalWith 'a' "arch" "ARCH" "architecture (default x86_64)" "x86_64"- <*> optionalWith auto 'e' "edition" "EDITION" "Fedora edition: workstation [default], server, ..." Workstation+ <*> optionalWith auto 'e' "edition" "EDITION" "Fedora edition: workstation [default]" Workstation <*> strArg "RELEASE" findISO :: Maybe String -> Bool -> String -> FedoraEdition -> String -> IO ()@@ -77,7 +89,7 @@ case tgtrel of "rawhide" -> (Nothing, "development/rawhide", Nothing, Just "Rawhide") -- FIXME: version hardcoding for respin, beta, and 30- "respin" -> (Just "https://dl.fedoraproject.org", "pub/alt/live-respins/", Just "F29-WORK-x86_64", Nothing)+ "respin" -> (Just "https://dl.fedoraproject.org", "pub/alt/live-respins/", Just ("F29-" <> liveRespin edition <> "-x86_64"), Nothing) "beta" -> (Nothing ,"releases/test/30_Beta", Nothing, Just "30_Beta") -- FIXME: hardcoding "30" -> (Nothing, "development/30", Nothing, Just "30") -- FIXME: hardcoding rel | all isDigit rel -> (Nothing, "releases" </> rel, Nothing, Just rel)@@ -89,9 +101,9 @@ toppath = if null ((decodePathSegments . extractPath) (B.pack host)) then "pub/fedora/linux" else ""- url = if isJust mlocn then host </> relpath else joinPath [host, toppath, relpath, show edition, arch, editionMedia edition <> "/"]- prefix = fromMaybe (intercalate "-" ([editionPrefix edition, arch] <> maybeToList mrelease)) mprefix- (fileurl, remotesize) <- findURL url prefix+ url = if isJust mlocn then host </> relpath else joinPath [host, toppath, relpath, if edition `elem` fedoraSpins then "Spins" else show edition, arch, editionMedia edition <> "/"]+ prefix = fromMaybe (intercalate "-" (["Fedora", show edition, editionType edition, arch] <> maybeToList mrelease)) mprefix+ (fileurl, remotesize, mchecksum) <- findURL url prefix dlDir <- getUserDir "DOWNLOAD" if dryrun then do@@ -102,31 +114,18 @@ setCurrentDirectory dlDir let localfile = takeFileName fileurl symlink = dlDir </> prefix <> "-latest" <.> takeExtension fileurl- exists <- doesFileExist localfile- if exists- then do- filestatus <- getFileStatus localfile- let localsize = fileSize filestatus- if Just (fromIntegral localsize) == remotesize- then do- putStrLn "File already fully downloaded"- updateSymlink localfile symlink- else do- canwrite <- writable <$> getPermissions localfile- unless canwrite $ error' "file does have write permission, aborting!"- downloadFile fileurl- updateSymlink localfile symlink- else do- downloadFile fileurl- updateSymlink localfile symlink+ downloadFile fileurl remotesize localfile+ fileChecksum mchecksum+ updateSymlink localfile symlink where- findURL :: String -> String -> IO (String, Maybe Integer)+ findURL :: String -> String -> IO (String, Maybe Integer, Maybe String) findURL url prefix = do mgr <- newManager tlsManagerSettings redirect <- httpRedirect mgr url let finalUrl = maybe url B.unpack redirect hrefs <- httpDirectory mgr finalUrl let mfile = listToMaybe $ filter (T.pack prefix `T.isPrefixOf`) hrefs :: Maybe Text+ mchecksum = listToMaybe $ filter (T.pack "CHECKSUM" `T.isSuffixOf`) hrefs case mfile of Nothing -> error' $ "not found " <> finalUrl@@ -134,7 +133,7 @@ let finalfile = finalUrl </> T.unpack file putStrLn finalfile size <- httpFileSize mgr finalfile- return (finalfile, size)+ return (finalfile, size, (finalUrl </>) . T.unpack <$> mchecksum) updateSymlink :: FilePath -> FilePath -> IO () updateSymlink target symlink =@@ -146,21 +145,52 @@ when (linktarget /= target) $ do removeFile symlink createSymbolicLink target symlink- else createSymbolicLink target symlink- putStrLn $ unwords [symlink, "->", target]+ putStrLn $ unwords [symlink, "->", target]+ else do+ createSymbolicLink target symlink+ putStrLn $ unwords [symlink, "->", target] - downloadFile :: String -> IO ()- downloadFile url =- unless dryrun $ cmd_ "curl" ["-C", "-", "-O", url]+ downloadFile :: String -> Maybe Integer -> String -> IO ()+ downloadFile url remotesize localfile = do+ exists <- doesFileExist localfile+ if exists+ then do+ localsize <- fileSize <$> getFileStatus localfile+ if Just (fromIntegral localsize) == remotesize+ then+ putStrLn "File already fully downloaded"+ else do+ canwrite <- writable <$> getPermissions localfile+ unless canwrite $ error' "file does have write permission, aborting!"+ unless dryrun $+ cmd_ "curl" ["-C", "-", "-O", url]+ else+ unless dryrun $+ cmd_ "curl" ["-C", "-", "-O", url] -editionPrefix :: FedoraEdition -> String-editionPrefix Workstation = "Fedora-Workstation-Live"-editionPrefix Server = "Fedora-Server-dvd"-editionPrefix Silverblue = "Fedora-Silverblue-ostree"-editionPrefix Everything = "Fedora-Everything-netinst"-editionPrefix Container = "Fedora-Container-Base"-editionPrefix _ = error' "Edition not yet supported"+ fileChecksum :: Maybe FilePath -> IO ()+ fileChecksum mchecksum =+ case mchecksum of+ Nothing -> return ()+ Just checksum -> do+ let local = takeFileName checksum+ exists <- doesFileExist local+ unless exists $+ cmd_ "curl" ["-C", "-", "-s", "-S", "-O", checksum]+ putStrLn $ "Running sha256sum on " <> local+ cmd_ "sha256sum" ["--check", "--ignore-missing", local] +editionType :: FedoraEdition -> String+editionType Server = "dvd"+editionType Silverblue = "ostree"+editionType Everything = "netinst"+editionType Container = "Base"+editionType _ = "Live"+ editionMedia :: FedoraEdition -> String editionMedia Container = "images" editionMedia _ = "iso"++liveRespin :: FedoraEdition -> String+liveRespin = take 4 . map toUpper . show+
README.md view
@@ -12,13 +12,11 @@ `fedora-img-dl rawhide` : downloads the latest Fedora Rawhide Workstation Live iso -`fedora-img-dl respin` : downloads the latest Live Workstation respin- `fedora-img-dl -e silverblue 30` : downloads Fedora 30 Silverblue iso -`fedora-img-dl --edition server --arch aarch64 29` : will bring down the F29 Server iso+`fedora-img-dl -e kde respin` : downloads the latest KDE Live respin -(Currently Spins are not yet supported.)+`fedora-img-dl --edition server --arch aarch64 29` : will bring down the F29 Server iso A symlink to the latest iso is also created: eg for rawhide it might be `Fedora-Workstation-Live-x86_64-Rawhide-latest.iso`.
fedora-img-dl.cabal view
@@ -1,6 +1,6 @@ cabal-version: 1.18 name: fedora-img-dl-version: 0.2+version: 0.3 synopsis: Fedora image download tool description: Tool to download Fedora iso and image files -- can change to GPL-3.0-or-later with Cabal-2