aboutsummaryrefslogtreecommitdiff
path: root/src/Main.hs
blob: 833c999737f9c21221adecf5a838ea34acdbb641 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
{-
Copyright (C) 2022 Yuchen Pei.

This file is part of fsd.

fsd is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

fsd is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
Public License for more details.

You should have received a copy of the GNU Affero General Public
License along with fsd.  If not, see <https://www.gnu.org/licenses/>.

-}

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE TypeOperators #-}

import System.Process hiding (runCommand)
import Data.List.Extra
import System.FilePath
import FSD.ChangeLog
import FSD.Download
import FSD.Control
import Control.Exception
import Control.Monad
import FSD.Copyright
import Data.Text (Text)
import Data.Text qualified as T
import Data.Maybe
import Database.SQLite.Simple
import FSD.Db
import Debian.Changes
import Debian.Control
import Options.Applicative
import FSD.Package
import FSD.PackageInfo
import FSD.Source
import Text.Show.Pretty hiding (List)
import FSD.Translation
import FSD.Types
import FSD.Wiki
import System.IO

main :: IO ()
main = do
  (options, command) <- execParser opts
  runCommand options command
  where
    opts = info (progParser <**> helper)
      ( fullDesc
     <> progDesc "FSD import utility"
     <> header "fsd - a tool to import from Debian and export to FSD entries.")

runCommand :: Options -> Command -> IO ()
runCommand opts cmd = withFsDb (database opts) $ \db ->
  case cmd of
    Init -> initConn db
    Get target -> do
      when (target == TAll || target == TSources) $
        downloadSources (distDir opts) >> return ()
      when (target == TAll || target == TPackages) $
        downloadPackages (distDir opts) >> return ()
      when (target == TAll || target == TTranslation) $
        downloadTranslation (distDir opts) >> return ()
      when (target == TAll || target == TChangelog) $
        getMetadataFiles db (distDir opts) (nWorkers opts) MTChangelog
        >> return ()
      when (target == TAll || target == TCopyright) $
        getMetadataFiles db (distDir opts) (nWorkers opts) MTCopyright
        >> return ()
    Import target -> do
      when (target == TAll || target == TSources) $
        readControl <$>
        (readFile $ (distDir opts) </> "Sources") >>= insertSources db
      when (target == TAll || target == TPackages) $
        readControl <$>
        (readFile $ (distDir opts) </> "Packages") >>= insertPackages db
      when (target == TAll || target == TTranslation) $
        readControl <$> (readFile $ (distDir opts) </> "Translation-en")
        >>= insertTranslations db
      when (target == TAll || target == TChangelog) $
        insertChangeLogEntries db (distDir opts)
      when (target == TAll || target == TCopyright) $
        insertCopyUps db (distDir opts)
    Export names ->
      if null names then exportWikiAll db (exportDir opts) else
        mapM_ (\name -> do
                  allTypes <- (getDbAllTypes (T.pack name) db)
                  when (isJust allTypes) $
                    exportWiki db (exportDir opts) (fromJust allTypes)) names
    List -> T.unpack . T.unlines <$> getDbPkgNames db >>= putStr
    otherwise -> error "Unimplemented"

{- USAGE
fsd init
fsd get sources | packages | translation | changelog | copyright | all
fsd import sources | packages | translation | changelog | copyright | all
fsd export [pkgNames]
fsd list
fsd clean  -- unimplemented
fsd show [--debug] <pkgName> -- unimplemented
-}

data Options = Options
  { database :: FilePath,
    distDir :: FilePath,
    nWorkers :: Int,
    exportDir :: FilePath
  }

data Command
  = Init -- consider creating directories during init
  | Get Target
  | Import Target
  | Export [String]
  | List
  | Clean
  | Show_ String

data Target
  = TAll
  | TSources
  | TPackages
  | TTranslation
  | TChangelog
  | TCopyright
  deriving Eq
  
progParser :: Parser (Options, Command)
progParser = (,) <$> optParser <*> cmdParser

optParser :: Parser Options
optParser
  = Options
  <$> strOption (long "database" <> short 'd' <> metavar "DATABASE"
                <> value "./directory.db" <> showDefault
                <> help "Database store of imported package info.")
  <*> strOption (long "dist-dir" <> short 'm' <> metavar "DISTDIR"
                <> value "./distfiles" <> showDefault
                <> help "Root directory to files downloaded from debian.")
  <*> option auto (long "nworkers" <> short 'n' <> metavar "NWORKERS"
                <> value 4 <> showDefault
                <> help "Number of wget workers for downloading changelog \
                        \and copyright files.")
  <*> strOption (long "export-dir" <> short 'e' <> metavar "EXPORTDIR"
                <> value "./wiki" <> showDefault
                <> help "Directory to place the exported .wiki files.")

cmdParser :: Parser Command
cmdParser = hsubparser
  $ command "init" (info (pure Init) $ progDesc "Initilise database")
  <> command "get" (info (Get <$> targetParser) $ progDesc
      "Download index and metadata files.")
  <> command "import" (info (Import <$> targetParser) $ progDesc
      "Import package info from local index and metadata files.")
  <> command "export" (info (Export <$> many pkgNameParser) $ progDesc
      "Export packages to .wiki files.")
  <> command "list" (info (pure List) $ progDesc "List imported packages")

target :: ReadM Target
target = eitherReader $ \s -> case s of
  "all" -> Right TAll
  "sources" -> Right TSources
  "packages" -> Right TPackages
  "translation" -> Right TTranslation
  "changelog" -> Right TChangelog
  "copyright" -> Right TCopyright
  otherwise -> Left "TARGET can be all, sources, packages, \
                    \translation, changelog or copyright"

targetParser :: Parser Target
targetParser = argument target (metavar "TARGET")

pkgNameParser :: Parser String
pkgNameParser = strArgument (metavar "PKGNAME")

getMetadataFiles :: FsDb -> FilePath -> Int -> MetadataType -> IO [ProcessHandle]
getMetadataFiles db root n mtype = do
  names <- getDbPkgNames db
  writeWgetListFiles root mtype names n
  downloadMetadataFiles root mtype n

insertSources :: FsDb -> Control -> IO ()
insertSources db control = do
  let sources = getSources control
  putStr $ "Importing " ++ show (length sources) ++ " sources..."
  hFlush stdout
  mapM_ (insertSource db) sources
  putStrLn "Done."

insertPackages :: FsDb -> Control -> IO ()
insertPackages db control = do
  let packages = getPackages control
  putStr $ "Importing " ++ show (length packages) ++ " packages..."
  hFlush stdout
  mapM_ (insertPackage db) packages
  putStrLn "Done."

insertTranslations :: FsDb -> Control -> IO ()
insertTranslations db control = do
  let translations = getTranslations control
  putStr $ "Importing " ++ show (length translations) ++ " translations..."
  hFlush stdout
  mapM_ (insertTranslation db) translations
  putStrLn "Done."

insertChangeLogEntries :: FsDb -> FilePath -> IO ()
insertChangeLogEntries db root = do
  paths <- getChangeLogPaths root
  putStr $ "Importing " ++ show (length paths) ++ " changelogs..."
  hFlush stdout
  mapM_ (\path -> do
            change' <- (readChangeLog <$> readFile path)
            when (isJust change') $
              insertChangeLogEntry db $ fromJust change') paths

getChangeLogPaths :: FilePath -> IO [FilePath]
getChangeLogPaths root = do
  lines <$>
    readCreateProcess
    (shell $ "find " ++ root ++ " -name 'stable_changelog' ") ""
  
insertChangeLogEntry :: FsDb -> ChangeLogEntry -> IO ()
insertChangeLogEntry db entry =
  let entry' = convertChangeLogEntry entry in
    when (isJust entry') (insertFSDChangeLogEntry db $ fromJust entry')

insertCopyUps :: FsDb -> FilePath -> IO ()
insertCopyUps db root = do
  pairs <- getCopyrightPathsAndNames root
  putStr $ "Importing " ++ show (length pairs) ++ " copyright files..."
  hFlush stdout
  mapM_ (\(name, path) ->
            catch (readControl <$> readFile' path >>= insertCopyUp db name)
            (\e ->
               hPutStr stderr
               ("Warning: error while processing " ++ path ++ ": " ++
                show (e :: SomeException) ++ "\n"))
        ) pairs

getNameFromMetadataPath :: FilePath -> Maybe Text
getNameFromMetadataPath path =
    case split (=='/') path of
      [] -> Nothing
      [x] -> Nothing
      xs -> Just $ T.pack $ last $ init xs

getCopyrightPathsAndNames :: FilePath -> IO [(Text, FilePath)]
getCopyrightPathsAndNames root = do
  paths <- lines <$>
    readCreateProcess
    (shell $ "find " ++ root ++ " -name 'stable_copyright' ") ""
  return $ catMaybes $ maybePair <$> paths
  where
    maybePair path = case getNameFromMetadataPath path of
      Just name -> Just (name, path)
      Nothing -> Nothing
  
insertCopyUp :: FsDb -> Text -> Control -> IO ()
insertCopyUp db package control = case parseCopyright package control of
  Nothing -> return ()
  Just (upstream, copyright) ->
    insertUpstream db upstream >> insertCopyright db copyright

exportWiki ::
  FsDb -> FilePath ->
  Source :. Package :. Translation :. FSDChangeLogEntry :. Upstream ->
  IO ()
exportWiki db dir (source :. package :. trans :. change :. upstream) = do
  copyright <- getDbCopyright (sPackage source) db
  wiki <- case copyright of
    Nothing -> return Nothing
    Just copyright' ->
      fmap formatWikiEntry
        <$> makePackageInfo source package trans change upstream copyright'
  when (isJust wiki) $
    writeFile (dir ++ "/" ++ (T.unpack $ sPackage source) ++ ".wiki")
      (T.unpack $ fromJust wiki)

exportWikiAll :: FsDb -> FilePath -> IO ()
exportWikiAll db dir = do
  allTypes <- getDbAll db
  putStr $ "Exporting up to " ++ show (length allTypes) ++ " entries..."
  hFlush stdout
  mapM_ (exportWiki db dir) allTypes


-- tests

testSources =
  getSources <$> readControl <$> (readFile "./distfiles/Sources-short") >>= pPrint

testPackages =
  getPackages <$> readControl <$> (readFile "./distfiles/Packages-short") >>= pPrint

testTranslation =
  getTranslations <$> readControl <$> (readFile "./distfiles/Translation-en-short") >>= pPrint

testCopyright = do
  result <- try (readFile' "./metadata.ftp-master.debian.org/changelogs/main/d/dbix-easy-perl/stable_copyright") :: IO (Either SomeException String)
  either (\ex -> pPrint ex) (\content -> (pPrint . parseCopyright "dbix-easy-perl" . readControl) content) result

testChangeLog :: IO ()
testChangeLog = do
  getChangeLogEntry <$> (readFile "./metadata.ftp-master.debian.org/changelogs/main/e/emacs/stable_changelog") >>= pPrint