-
Notifications
You must be signed in to change notification settings - Fork 0
/
fhpkg.hs
737 lines (668 loc) · 29.1 KB
/
fhpkg.hs
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
{-# LANGUAGE CPP #-}
-- |
-- Module : Packages
-- Copyright : (C) 2017-2019 Jens Petersen
--
-- Maintainer : Jens Petersen <petersen@fedoraproject.org>
-- Stability : alpha
--
-- Explanation: Cloning and pulling package git repos
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
#if (defined(MIN_VERSION_base) && MIN_VERSION_base(4,13,0))
#else
import Control.Applicative (optional, some, (<|>)
#if (defined(MIN_VERSION_base) && MIN_VERSION_base(4,8,0))
#else
, pure, (<$>), (<*>)
#endif
)
#endif
import Control.Monad (filterM, unless, when, (>=>))
import Control.Monad.Extra (mapMaybeM, unlessM, whenJustM)
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Char8 as BL
import Data.Char (isDigit)
import Data.Maybe
import Data.List
import Data.List.Split (splitOn)
import Network.HTTP.Simple
import Network.HTTP.Types
import System.Directory (doesDirectoryExist, doesFileExist,
getCurrentDirectory, getHomeDirectory,
#if (defined(MIN_VERSION_directory) && MIN_VERSION_directory(1,2,5))
listDirectory,
#else
getDirectoryContents,
#endif
setCurrentDirectory)
import System.FilePath ((</>), (<.>), takeFileName)
import System.IO (BufferMode(..), hIsTerminalDevice, hSetBuffering, stdout)
--import System.Posix.Env (getEnv)
import Text.CSV (parseCSV)
import Distribution.Fedora (Dist(..), distBranch, distRepo, distUpdates,
getLatestFedoraDist, getRawhideDist)
--import Distribution.Fedora.Branch (Branch(..))
import SimpleCmd
#if !MIN_VERSION_simple_cmd(0,2,3)
import System.Exit
import qualified System.Process as P
#endif
import SimpleCmd.Git
import SimpleCmdArgs
import Build (build, readBuildCmd)
import Dist (distArg, distRemote, hackageRelease, ltsStream)
import Koji (rpkg)
import Paths_fedora_haskell_tools (version)
import RPM (buildRequires, haskellSrcPkgs, Package, pkgDir,
repoquery, rpmspec, rqfnewline)
import Utils (checkPkgsGit, withCurrentDirectory)
#if (defined(MIN_VERSION_directory) && MIN_VERSION_directory(1,2,5))
#else
listDirectory :: FilePath -> IO [FilePath]
listDirectory path =
filter f <$> getDirectoryContents path
where f filename = filename /= "." && filename /= ".."
#endif
main :: IO ()
main = do
hSetBuffering stdout NoBuffering
cwd <- getCurrentDirectory
branched <- getLatestFedoraDist
simpleCmdArgs (Just version) "Fedora Haskell packages tool"
"Fedora packages maintenance tool" $
subcommands . sort $
[ Subcommand "checkout" "fedpkg switch-branch" $
repoAction_ branched True False (return ()) <$> distArg <*> pkgArgs
, Subcommand "clone" "clone repos" $
clone branched <$> branching <*> distArg <*> pkgArgs
, Subcommand "clone-new" "clone new packages" $
cloneNew branched <$> branching <*> distArg
, Subcommand "cblrpm" "Run cblrpm command" $
cblrpm branched <$> strOptionWith 'c' "cmd" "CMD" "command to execute" <*> distArg <*> pkgArgs
, Subcommand "cmd" "arbitrary command (with args)" $
execCmd branched <$> strOptionWith 'c' "cmd" "CMD" "command to execute" <*> distArg <*> pkgArgs
, Subcommand "count" "count number of packages" $
(repoqueryHaskellPkgs branched False >=> (print . length)) <$> distArg
, Subcommand "depends" "cabal-depends" $
repoAction branched False (Output cabalDepends) <$> distArg <*> pkgArgs
, Subcommand "diff" "git diff" $
gitDiff branched <$> optional gitFormat
<*> optional (strOptionWith 'w' "with-branch" "BRANCH" "Branch to compare")
<*> distArg <*> pkgArgs
, Subcommand "diff-origin" "git diff origin" $
gitDiffOrigin branched <$> distArg <*> pkgArgs
-- , Subcommand "diff-branch" "compare branch with master" $
-- repoAction branched True (Header False compareRawhide) <$> distArg <*> pkgArgs
, Subcommand "diffstat" "Show diffstat output" $
repoAction branched False (Output (const diffStat)) <$> distArg <*> pkgArgs
, Subcommand "hackage-upload" "upload Hackage distro data" $
hackageUpload branched <$> switchRefresh
, Subcommand "hackage-compare" "compare with Hackage distro data" $
hackageCompare branched <$> switchRefresh
-- more or less the same as 'pushed'
, Subcommand "head-origin" "packages with head in sync with origin" $
headOrigin branched <$> distArg <*> pkgArgs
, Subcommand "leaf" "list leaf packages (slow!)" $
leaves branched <$> switchWith 'v' "deps" "show also deps" <*> distArg <*> pkgArgs
, Subcommand "list" "list packages that BR ghc-Cabal-devel" $
(repoqueryHaskellPkgs branched False >=> putStrList) <$> distArg
, Subcommand "merge" "git merge" $
merge branched <$> strOptionWith 'f' "from" "BRANCH" "specify branch to merge from" <*> distArg <*> pkgArgs
, Subcommand "missing" "missing dependency source packages" $
missingDeps branched <$> distArg <*> pkgArgs
, Subcommand "new" "unbuilt packages" $
(newPackages branched >=> putStrList) <$> distArg
, Subcommand "old-packages" "packages not in repoquery" $
oldPackages branched <$> distArg <*> pkgArgs
, Subcommand "prep" "fedpkg prep" $
prep branched <$> distArg <*> pkgArgs
, Subcommand "stackage-compare" "compare with stackage" $
stackageCompare branched <$> streamOpt <*> stackageOpts <*> distArg <*> pkgArgs
, Subcommand "commit" "fedpkg commit" $
commit branched <$> strOptionWith 'm' "message" "COMMITMSG" "commit message" <*> distArg <*> pkgArgs
, Subcommand "fetch" "git fetch repos" $
repoAction_ branched True False (git_ "fetch" []) <$> distArg <*> pkgArgs
, Subcommand "pull" "git pull repos" $
repoAction_ branched True False (git_ "pull" ["--rebase"]) <$> distArg <*> pkgArgs
, Subcommand "push" "git push repos" $
repoAction_ branched True False (git_ "push" []) <$> distArg <*> pkgArgs
, Subcommand "pushed" "show git pushed packages" $
pushed branched <$> distArg <*> pkgArgs
, Subcommand "refresh" "cabal-rpm refresh" $
refresh branched <$> switchWith 'n' "dry-run" "Show patch but don't apply" <*> distArg <*> pkgArgs
, Subcommand "remaining" "remaining packages to be built in TAG" $
remaining <$> switchWith 'c' "count" "show many packages left" <*> strArg "TAG" <*> pkgArgs
, Subcommand "subpkgs" "list subpackages" $
repoAction branched True (Header True (\ p -> rpmspec [] (Just "%{name}-%{version}") (p <.> "spec") >>= putStrList)) <$> distArg <*> pkgArgs
, Subcommand "subpackaged" "list subpackaged libraries" $
subpackaged branched <$> switchWith 'V' "show-versions" "Show versions" <*> distArg <*> pkgArgs
, Subcommand "tagged" "list koji DIST tagged builds" $
listTagged_ <$> switchWith 's' "short" "list packages not builds" <*> strArg "TAG"
, Subcommand "unbranched" "packages without this branch" $
unbranched branched <$> distArg <*> pkgArgs
, Subcommand "unpushed" "show unpushed commits" $
unpushed branched <$> switchWith 's' "short" "no log" <*> distArg <*> pkgArgs
, Subcommand "update" "cabal-rpm update" $
update branched <$> keepOpt <*> streamOpt <*> distArg <*> pkgArgs
, Subcommand "verrel" "show nvr of packages" $
verrel branched <$> distArg <*> pkgArgs] ++
map (buildCmd cwd) [ ("install", "build locally and install")
, ("mock", "build in mock")
, ("chain", "build deps recursively in Koji")
, ("koji", "build in Koji (deprecated) without checking dependencies")
, ("pending", "show planned changes")
, ("changed", "show changed pkgs")
, ("built", "show pkgs whose NVR already built")
, ("bump", "bump release for NVRs already built")
, ("not-installed", "list packages not locally installed")
]
where
pkgArgs = some (removeSuffix "/" <$> strArg "PKG...")
branching = switchWith 'B' "branches" "clone branch dirs (fedpkg clone -B)"
gitFormat :: Parser DiffFormat
gitFormat =
flagWith' DiffShort 's' "short" "Just output package name" <|>
DiffContext <$> optionWith auto 'u' "unified" "CONTEXT" "Lines of context"
buildCmd cwd (c, desc) =
Subcommand c desc $
build cwd Nothing False (readBuildCmd c) <$> distArg <*> pkgArgs
switchRefresh = switchWith 'r' "refresh" "repoquery --refresh"
streamOpt = strOptionalWith 's' "stream" "STACKAGESTREAM" ("Stackage stream [" ++ ltsStream ++ "]") ltsStream
stackageOpts :: Parser StkgOpt
stackageOpts =
flagWith' StkgMissing 'm' "missing" "only list missing packages" <|>
flagWith StkgAll StkgOnly 'o' "only" "only Stackage packages"
keepOpt = switchWith 'k' "keep-going" "Keep going after an error"
data DiffFormat =
DiffShort | DiffContext Int
deriving (Eq)
data StkgOpt = StkgAll | StkgOnly | StkgMissing
deriving Eq
putStrList :: [String] -> IO ()
putStrList =
putStr . unlines
-- should make separate rhel client so -B does not need dist
clone :: Dist -> Bool -> Dist -> [Package] -> IO ()
clone _ True dist pkgs = cloneAllBranches dist pkgs
clone branched False dist pkgs =
repoAction_ branched True False (return ()) dist pkgs
cloneNew :: Dist -> Bool -> Dist -> IO ()
cloneNew branched True dist = do
rawhide <- getRawhideDist
newPackages branched rawhide >>= cloneAllBranches dist
cloneNew branched False dist =
newPackages branched dist >>= repoAction_ branched True False (return ()) dist
execCmd :: Dist -> String -> Dist -> [Package] -> IO ()
execCmd _ "" _ _ = error "CMD string must be given"
execCmd branched cs dist pkgs =
repoAction_ branched True True (shell_ cs) dist pkgs
gitDiff :: Dist -> Maybe DiffFormat -> Maybe String -> Dist -> [Package] -> IO ()
gitDiff branched (Just DiffShort) mbrnch =
repoAction branched False (Header False doGitDiff)
where
doGitDiff pkg = do
let branch = maybeToList mbrnch
out <- git "diff" branch
unless (null out) $ putStrLn pkg
gitDiff branched fmt mbrnch =
repoAction branched False (Output (const doGitDiff))
where
doGitDiff = do
let branch = maybeToList mbrnch
contxt = case fmt of
(Just (DiffContext n)) -> ["--unified=" ++ show n]
_ -> []
out <- git "diff" $ branch ++ contxt
return $ if null out then "" else out
gitDiffOrigin :: Dist -> Dist -> [Package] -> IO ()
gitDiffOrigin branched dist =
repoAction branched False (Output (const (git "diff" [distRemote branched dist]))) dist
-- FIXME option only to show PVP X.Y bumps or X.Y.Z bumps
stackageCompare :: Dist -> String -> StkgOpt -> Dist -> [Package] -> IO ()
stackageCompare branched stream opt dist =
repoAction branched True (Header False compareStackage) dist
where
compareStackage :: Package -> IO ()
compareStackage p = do
nvr <- cmd (rpkg dist) ["verrel"]
let hkg = removePrefix "ghc-" p
(ok,_out,err) <- cmdFull "stack" ["--resolver" , stream, "list", hkg] ""
-- FIXME rename to mstkgver?
let stkg =
if ok then
case filter (not . ("Selected resolver:" `isPrefixOf`)) (lines err) of
[nv] -> Just (removePrefix (hkg ++ "-") nv)
_ -> Nothing
else Nothing
let same = isJust stkg && (fromJust stkg ++ "-") `isInfixOf` nvr
unless same $
if opt == StkgMissing
then when (isNothing stkg) $ putStrLn p
else
if isNothing stkg
then unless (opt == StkgOnly) $
putStrLn $ stream ++ " missing: " ++ removePrefix "ghc-" p
else do
putStrLn nvr
putStrLn $ replicate (length (dropVerrel nvr) + 1) ' ' ++ fromJust stkg +-+ "(" ++ stream ++ ")"
diffStat :: IO String
diffStat = git "diff" ["--stat"]
hackageUpload :: Dist -> Bool -> IO ()
hackageUpload branched refreshData = do
csv <- repoqueryHackageCSV hackageRelease
home <- getHomeDirectory
[username, password] <- map B.pack . words <$> readFile (home </> ".fedora/hackage.auth")
req <- setRequestBasicAuth username password .
setRequestBodyLBS (BL.pack csv) .
addRequestHeader hContentType (B.pack "text/csv") .
setRequestMethod methodPut <$>
parseRequestThrow "https://hackage.haskell.org/distro/Fedora/packages.csv"
resp <- httpLbs req
BL.putStrLn $ getResponseBody resp
where
repoqueryHackageCSV :: Dist -> IO String
repoqueryHackageCSV dist = do
pkgs <- repoqueryHackages branched hackageRelease
-- Hackage csv chokes on a final newline
intercalate "\n" . sort . map (replace "\"ghc-" "\"") . lines <$>
repoquery dist (["--repo=fedora", "--repo=updates", "--latest-limit=1", "--qf=\"%{name}\",\"%{version}\",\"https://src.fedoraproject.org/rpms/%{source_name}\"" ++ rqfnewline] ++ ["--refresh" | refreshData] ++ pkgs)
hackageCompare :: Dist -> Bool -> IO ()
hackageCompare branched refreshData =
repoqueryHackages branched hackageRelease >>=
compareHackage hackageRelease
where
compareHackage :: Dist -> [Package] -> IO ()
compareHackage dist pkgs' = do
req <- addRequestHeader hContentType (B.pack "text/csv") <$>
parseRequestThrow "https://hackage.haskell.org/distro/Fedora/packages.csv"
hck <- getResponseBody <$> httpLbs req
let hackage = sort . either (error "Malformed Hackage csv") (map mungeHackage) $ parseCSV "packages.csv" (BL.unpack hck)
repoquery dist (["--repo=fedora", "--repo=updates", "--latest-limit=1", "--qf=%{name},%{version}" ++ rqfnewline] ++ ["--refresh" | refreshData] ++ pkgs') >>=
compareSets True hackage . sort . map mungeRepo . lines
mungeHackage :: [String] -> PkgVer
mungeHackage [n,v,_] = PV n v
mungeHackage _ = error "Malformed Hackage csv"
mungeRepo :: String -> PkgVer
mungeRepo s | ',' `elem` s =
let (p,v) = break (== ',') s in
PV (removePrefix "ghc-" p) (tail v)
| otherwise = error "Malformed repoquery output"
compareSets :: Bool -> [PkgVer] -> [PkgVer] -> IO ()
compareSets _ [] [] = return ()
compareSets _ [] (f:fs) = do
putStrLn (show f +-+ "(new)")
compareSets False [] fs
compareSets all' (h:hs) [] = do
when all' $ putStrLn ("Removed:" +-+ show h)
compareSets all' hs []
compareSets all' (h:hs) (f:fs) | h == f = compareSets all' hs fs
| h < f = do
when all' $ putStrLn $ "Removed:" +-+ show h
compareSets all' hs (f:fs)
| h > f = do
putStrLn $ show f +-+ "(new)"
compareSets all' (h:hs) fs
| otherwise = do
putStrLn $ pvPkg h ++ ":" +-+ pvVer h +-+ "->" +-+ pvVer f
compareSets all' hs fs
headOrigin :: Dist -> Dist -> [Package] -> IO ()
headOrigin branched dist =
repoAction branched False (Header False gitHeadAtOrigin) dist
where
gitHeadAtOrigin :: Package -> IO ()
gitHeadAtOrigin pkg = do
-- use gitDiffQuiet
same <- gitBool "diff" ["--quiet", distRemote branched dist ++ "..HEAD"]
when same $ putStrLn pkg
-- FIXME does not take static requires into account
leaves :: Dist -> Bool -> Dist -> [Package] -> IO ()
leaves branched verb =
repoAction branched True (Header verb checkLeafPkg)
where
-- FIXME: make a dependency cache
checkLeafPkg :: Package -> IO ()
checkLeafPkg pkg = do
dir <- takeFileName <$> getCurrentDirectory
let branchdir = dir /= pkg
top = if branchdir then "../.." else ".."
spec = pkg <.> "spec"
subpkgs <- rpmspec ["--builtrpms"] (Just "%{name}") spec
allpkgs <- listDirectory top
let other = map (\ p -> top </> p </> (if branchdir then dir else "") </> p <.> "spec") $ allpkgs \\ [pkg]
found <- filterM (dependsOn subpkgs) other
if null found
then putStrLn pkg
else when verb $ putStrList found
where
dependsOn :: [Package] -> Package -> IO Bool
dependsOn subpkgs p = do
file <- doesFileExist p
if file
then do
deps <- buildRequires p
return $ any (`elem` deps) subpkgs
else return False
merge :: Dist -> String -> Dist -> [Package] -> IO ()
merge branched branch =
repoAction_ branched True False (git_ "merge" [branch])
missingDeps :: Dist -> Dist -> [Package] -> IO ()
missingDeps branched dist =
repoAction branched True (Output checkForMissingDeps) dist
where
checkForMissingDeps :: Package -> IO String
checkForMissingDeps pkg = do
dir <- takeFileName <$> getCurrentDirectory
let top = if dir == pkg then ".." else "../.."
spec = pkg <.> "spec"
hasSpec <- doesFileExist spec
if hasSpec
then do
deps <- buildRequires (pkg <.> "spec") >>= haskellSrcPkgs top dist
unlines <$> filterM (noPkgDir top) deps
else putStrLn ("no " ++ pkg ++ ".spec file found!") >> return ""
where
noPkgDir :: FilePath -> Package -> IO Bool
noPkgDir top dep =
not <$> doesDirectoryExist (top </> dep)
oldPackages :: Dist -> Dist -> [Package] -> IO ()
oldPackages branched dist pkgs = do
repopkgs <- repoqueryHaskellPkgs branched True dist
putStrList (pkgs \\ repopkgs)
prep :: Dist -> Dist -> [Package] -> IO ()
prep branched dist =
repoAction_ branched True True (cmd_ (rpkg dist) ["prep"]) dist
commit :: Dist -> String -> Dist -> [Package] -> IO ()
commit branched logmsg dist =
repoAction branched False (Output (const commitChanges)) dist
where
commitChanges :: IO String
commitChanges = do
nochgs <- gitDiffQuiet []
if nochgs
then return ""
else cmd (rpkg dist) ["commit", "-m", logmsg]
unpushed :: Dist -> Bool -> Dist -> [Package] -> IO ()
unpushed branched nolog dist =
repoAction branched True (Header False gitLogOneLine) dist
where
gitLogOneLine :: Package -> IO ()
gitLogOneLine pkg = do
out <- git "log" [distRemote branched dist ++ "..HEAD", "--pretty=oneline"]
unless (null out) $
putStrLn $ pkg ++ if nolog then "" else (unwords . map replaceHash . words) out
where
replaceHash h = if length h /= 40 then h else ":"
pushed :: Dist -> Dist -> [Package] -> IO ()
pushed branched dist =
repoAction branched True (Header False checkPushed) dist
where
checkPushed :: Package -> IO ()
checkPushed pkg = do
out <- git "log" [distRemote branched dist ++ "..HEAD", "--pretty=oneline"]
when (null out) $ putStrLn pkg
verrel :: Dist -> Dist -> [Package] -> IO ()
verrel branched dist =
repoAction_ branched False True (cmd_ (rpkg dist) ["verrel"]) dist
data PkgVer = PV { pvPkg :: String, pvVer :: String}
deriving (Eq)
instance Show PkgVer
where
show (PV p v) = p ++ "-" ++ v
instance Ord PkgVer
where
compare (PV p _) (PV p' _) = compare p p'
replace :: Eq a => [a] -> [a] -> [a] -> [a]
replace a b s@(x:xs) =
if a `isPrefixOf` s
then b ++ replace a b (drop (length a) s)
else x:replace a b xs
replace _ _ [] = []
repoqueryHaskellPkgs :: Dist -> Bool -> Dist -> IO [Package]
repoqueryHaskellPkgs branched verbose dist = do
when verbose $ do
tty <- hIsTerminalDevice stdout
when tty $ warning "Getting packages from repoquery"
let repo = distRepo branched dist
updates = maybeToList $ distUpdates branched dist
bin <- words <$> repoquery dist (["--repo=" ++ repo ++ "-source"] ++ ["--repo=" ++ u ++ "-source" | u <- updates] ++ ["--qf=%{name}" ++ rqfnewline, "--whatrequires", "ghc-Cabal-*"])
when (null bin) $ error "No packages using ghc-Cabal-devel found!"
return $ sort $ filter (not . isGhcXY) $ nub bin
where
isGhcXY :: String -> Bool
isGhcXY p = let prefix = head (splitOn "-" p)
in all (\c -> isDigit c || c == '.') (removePrefix "ghc" prefix)
repoqueryHackages :: Dist -> Dist -> IO [Package]
repoqueryHackages branched dist = do
srcs <- repoqueryHaskellPkgs branched False dist
libs <- repoqueryHaskellLibs False
let binsrcs = filter (not . ("ghc-" `isPrefixOf`)) srcs
sublibs = libs \\ map ("ghc-" ++) binsrcs
return $ sort $ nub (delete "haskell-platform" srcs ++ sublibs)
where
repoqueryHaskellLibs :: Bool -> IO [Package]
repoqueryHaskellLibs verbose = do
when verbose $ putStrLn "Getting libraries from repoquery"
let repo = distRepo branched dist
updates = maybeToList $ distUpdates branched dist
bin <- words <$> repoquery dist (["--repo=" ++ repo] ++ ["--repo=" ++ u | u <- updates] ++ ["--qf=%{name}" ++ rqfnewline, "--whatprovides", "libHS*-ghc*.so()(64bit)"])
when (null bin) $ error "No libHS*.so providers found!"
return $ sort $ filter ("ghc-" `isPrefixOf`) $ nub bin
newPackages :: Dist -> Dist -> IO [Package]
newPackages branched dist = do
ps <- repoqueryHaskellPkgs branched True dist
pps <- cmdLines "pagure" ["list", "--namespace", "rpms", "ghc*"]
local <- listDirectory "."
filterM (\ d -> not <$> doesFileExist (d </> "dead.package")) $ nub (pps ++ ps) \\ (local ++ ["Agda-stdlib", "ghc", "ghc-rpm-macros", "ghc-srpm-macros", "haskell-platform"])
haveSshKey :: IO Bool
haveSshKey = do
home <- getHomeDirectory
doesFileExist $ home </> ".ssh/id_rsa"
cloneAllBranches :: Dist -> [Package] -> IO ()
cloneAllBranches _ [] = return ()
cloneAllBranches dist (pkg:rest) = do
withCurrentDirectory "." $ do
putStrLn $ "\n==" +-+ pkg +-+ "=="
-- muser <- getEnv "USER"
haveSSH <- haveSshKey
dirExists <- doesDirectoryExist pkg
unless dirExists $
cmd_ (rpkg dist) $ ["clone"] ++ ["-a" | not haveSSH] ++ ["-B", pkg]
singleDir <- isGitDir pkg
when singleDir $
error "branch checkout already exists!"
cloneAllBranches dist rest
data Action = Output (Package -> IO String) | Header Bool (Package -> IO ())
showHeader :: Action -> Bool
showHeader (Header b _) = b
showHeader (Output _) = False
repoAction :: Dist -> Bool -> Action -> Dist -> [Package] -> IO ()
repoAction _ _ _ _ [] = return ()
repoAction branched needsSpec action dist (pkg:rest) = do
withCurrentDirectory "." $ do
let branch = distBranch branched dist
when (showHeader action) $
putStrLn $ "\n==" +-+ pkg +-+ branch +-+ "=="
-- muser <- getEnv "USER"
haveSSH <- haveSshKey
fileExists <- doesFileExist pkg
if fileExists
then error $ pkg +-+ "is a file"
else do
dirExists <- doesDirectoryExist pkg
unless dirExists $
cmd_ (rpkg dist) $ ["clone"] ++ ["-a" | not haveSSH] ++ ["-b", branch, pkg]
singleDir <- isGitDir pkg
unless singleDir $ do
branchDir <- doesDirectoryExist $ pkg </> branch
unless branchDir $
withCurrentDirectory pkg $
cmd_ (rpkg dist) ["clone", "-b", branch, pkg, branch]
wd <- pkgDir pkg branch ""
setCurrentDirectory wd
pkggit <- do
gd <- isGitDir "."
if gd
then checkPkgsGit
else return False
unless pkggit $
error $ "not a Fedora pkg git dir!:" +-+ wd
actual <- gitBranch
when (branch /= actual) $
whenJustM (cmdMaybe (rpkg dist) ["switch-branch", branch]) $ \ out ->
when (showHeader action) $ putStrLn out
currentBranch <- gitBranch
when (branch == currentBranch) $
unlessM (doesFileExist "dead.package") $ do
let spec = pkg <.> "spec"
hasSpec <- doesFileExist spec
-- FIXME: silence for cmds that only output package names (eg unpushed -s)
when (not hasSpec && showHeader action) $
putStrLn "No spec file!"
unless (needsSpec && not hasSpec) $
case action of
Header _ act -> act pkg
Output act -> do
out <- act pkg
unless (null out) $ do
putStrLn $ "\n==" +-+ pkg +-+ branch +-+ "=="
putStrLn out
repoAction branched needsSpec action dist rest
-- io independent of package
repoAction_ :: Dist -> Bool -> Bool -> IO () -> Dist -> [Package] -> IO ()
repoAction_ branched header needsSpec action =
repoAction branched needsSpec (Header header (const action))
-- compareRawhide :: Package -> IO ()
-- compareRawhide p = do
-- let spec = p <.> "spec"
-- nvr <- removeDisttag . unwords <$> rpmspec ["--srpm"] (Just "%{name}-%{version}-%{release}") spec
-- nvr' <- withBranch "master" $ do
-- haveSpec <- doesFileExist spec
-- unless haveSpec $ cmdSilent "git" ["pull"]
-- removeDisttag . unwords <$> rpmspec ["--srpm"] (Just "%{name}-%{version}-%{release}") spec
-- if nvr == nvr'
-- then putStrLn nvr
-- else do
-- putStrLn nvr
-- putStrLn nvr'
-- putStrLn ""
-- where
-- removeDisttag = reverse . tail . dropWhile (/= '.') . reverse
isFromHackage :: Package -> IO Bool
isFromHackage pkg =
grep_ "hackage.haskell.org/package/" $ pkg <.> "spec"
update :: Dist -> Bool -> String -> Dist -> [Package] -> IO ()
update branched keep stream =
repoAction branched True (Header True doUpdate)
where
doUpdate :: Package -> IO ()
doUpdate pkg = do
hckg <- isFromHackage pkg
if hckg
then do
ok <- cmdBool "cabal-rpm" ["update", "-s", stream]
unless (ok || keep) $
error' $ "cabal-rpm update failed for: " ++ pkg
else putStrLn "skipping since not hackage"
refresh :: Dist -> Bool -> Dist -> [Package] -> IO ()
refresh branched dryrun =
repoAction branched True (Header True refreshPkg)
where
refreshPkg :: Package -> IO ()
refreshPkg pkg = do
hckg <- isFromHackage pkg
if hckg
then cmd_ "cabal-rpm" $ "refresh" : ["--dry-run" | dryrun]
else putStrLn "skipping since not hackage"
listTagged_ :: Bool -> String -> IO ()
listTagged_ short tag =
listTagged short tag >>= putStrList
listTagged :: Bool -> String -> IO [String]
listTagged short tag = do
builds <- map (head . words) <$> cmdLines "koji" ["list-tagged", "--quiet", tag]
return $ nub $ map (if short then dropVerrel else id) builds
dropVerrel :: String -> String
dropVerrel nvr =
let parts = splitOn "-" nvr in
intercalate "-" $ take (length parts - 2) parts
remaining :: Bool -> String -> [Package] -> IO ()
remaining count tag pkgs = do
built <- listTagged True tag
let left = pkgs \\ built
if count
then print $ length left
else cmd_ "rpmbuild-order" $ ["sort", "-p"] ++ left
cabalDepends :: Package -> IO String
cabalDepends p = do
hckg <- isFromHackage p
if hckg then do
vr <- removePrefix "ghc-" . head <$>
rpmspec ["--srpm"] (Just "%{name}-%{version}") (p <.> "spec")
setCurrentDirectory vr
cmdQuiet "cabal-depends" ["--not-build", "--unique"]
else return ""
cblrpm :: Dist -> String -> Dist -> [Package] -> IO ()
cblrpm _ "" = error "CMD string must be given"
cblrpm branched cs =
repoAction branched True (Header True doCblRpm)
where
doCblRpm :: Package -> IO ()
doCblRpm p = do
hckg <- isFromHackage p
when hckg $
cmd_ "cblrpm" [cs]
unbranched :: Dist -> Dist -> [Package] -> IO ()
unbranched branched dist =
mapM_ checkBranch
where
checkBranch :: Package -> IO ()
checkBranch pkg =
withCurrentDirectory pkg $ do
dead <- doesFileExist "dead.package"
unless dead $ do
let distbranch = distBranch branched dist
branch <- gitBool "show-ref" ["--verify", "--quiet", "refs/heads/" ++ distbranch]
unless branch $ do
remotebranch <- gitBool "ls-remote" ["--exit-code", "--refs", "origin", distbranch]
unless remotebranch $
putStrLn pkg
subpackaged :: Dist -> Bool -> Dist -> [Package] -> IO ()
subpackaged branched versions dist pkgs = do
repoAction branched True (Output listSubpkgs) dist pkgs
where
listSubpkgs pkg = do
msubpkgs <- fmap (drop 2 . words) <$> cmdMaybe "grep" ["%global subpkgs", pkg <.> "spec"]
case msubpkgs of
Nothing -> return ""
Just subpkgs ->
intercalate "\n" <$> mapMaybeM expand subpkgs
where
expand subpkg =
let macro = (init . drop 2) subpkg in
fmap (removeVersion . last . words) <$> cmdMaybe "grep" ["%global " ++ macro, pkg <.> "spec"]
removeVersion nv =
if versions then nv else init (dropWhileEnd (/= '-') nv)
#if !MIN_VERSION_simple_cmd(0,2,2)
-- | 'gitBool c args' runs git command and return result
gitBool :: String -- ^ git command
-> [String] -- ^ arguments
-> IO Bool -- ^ result
gitBool c args = do
mout <- cmdMaybe "git" (c:args)
return $ isJust mout
#endif
#if !MIN_VERSION_simple_cmd(0,2,3)
cmdFull :: String -> [String] -> String -> IO (Bool, String, String)
cmdFull c args input = do
(ret, out, err) <- P.readProcessWithExitCode c args input
return (ret == ExitSuccess, removeTrailingNewline out, removeTrailingNewline err)
where
removeTrailingNewline :: String -> String
removeTrailingNewline "" = ""
removeTrailingNewline str' =
if last str' == '\n'
then init str'
else str'
#endif