-
Notifications
You must be signed in to change notification settings - Fork 20
/
RemoteId.hs
526 lines (490 loc) · 14.7 KB
/
RemoteId.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
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections #-}
module Portage.Metadata.RemoteId
(
-- * Remote id
RemoteId (..)
-- * Pretty printing
, prettyPrintRemoteIds
, prettyPrintRemoteId
-- * Parsing
, URIParser (..)
, matchURIs
, matchURI
, runUriParser
, definedParsers
-- ** Individual parsers
, hackageParser
, bitbucketParser
, codebergParser
, freedesktopParser
, gentooParser
, githubParser
, gitlabParser
, gnomeParser
, kdeParser
, launchpadParser
, osdnParser
, savannahParser
, savannahNonGnuParser
, sourceforgeParser
, sourcehutParser
-- ** Utility
-- *** URI scheme
, httpScheme
-- *** URI domain
, Domain
, domainOrWWW
, subdomain
-- *** URI path
, Path
, stripPrefix
, stripPrefixP
, gitPath
-- *** Misc
, ignore
, allChars
) where
import Control.Monad
import Data.Foldable (asum)
import qualified Data.List as L
import Data.Maybe (mapMaybe)
import qualified Data.Set as S
import Network.URI (URI(..), URIAuth(..), parseURI)
import System.FilePath.Posix
import Text.Parsec
import Text.Parsec.String
-- | Many of these are unlikely to source any Haskell code, but they are added
-- for completeness. The full list can be seen at:
-- <https://github.com/pkgcore/pkgcheck/blob/master/src/pkgcheck/checks/network.py>
data RemoteId
= RemoteIdHackage String -- ^ Hackage package
| RemoteIdBitbucket String String -- ^ Bitbucket project
| RemoteIdCodeberg String String -- ^ Codeberg project
| RemoteIdFreedesktop String String -- ^ Freedesktop GitLab project
| RemoteIdGentoo String -- ^ Gentoo project
| RemoteIdGithub String String -- ^ Github user and repo
| RemoteIdGitlab String String -- ^ Gitlab user and repo
| RemoteIdGnome String String -- ^ Gnome GitLab project
| RemoteIdKDE String String -- ^ KDE Invent project
| RemoteIdLaunchpad String -- ^ Launchpad project
| RemoteIdOSDN String -- ^ OSDN project
| RemoteIdSavannah String -- ^ GNU Savannah project
| RemoteIdSavannahNonGNU String -- ^ NonGNU Savannah project
| RemoteIdSourceforge String -- ^ Sourceforge project
| RemoteIdSourcehut String String -- ^ sourcehut project
deriving (Show, Eq, Ord)
-- | A set of parsers to use on a 'URI'. Each parser can produce an arbitrary
-- type. These intermediate types are coalesced in 'mkRemoteId' and
-- are hidden from the top-level using the @ExistentialQuantification@
-- language extension.
data URIParser = forall scheme user regname port path query fragment. URIParser
{ schemeParser :: Parser scheme -- | ^ scheme
, userParser :: Parser user -- | ^ user
, regnameParser :: Parser regname -- | ^ domain
, portParser :: Parser port -- | ^ port
, pathParser :: Parser path -- | ^ path
, queryParser :: Parser query -- | ^ query
, fragmentParser :: Parser fragment -- | ^ fragment
-- | coalescing function
, mkRemoteId :: scheme -> user -> regname -> port -> path -> query -> fragment -> RemoteId
}
type Domain = String
type Path = String
-- | Pretty print a 'S.Set' of 'RemoteId's as XML. Wraps the block in @"<upstream>"@.
prettyPrintRemoteIds :: S.Set RemoteId -> [String]
prettyPrintRemoteIds set
| S.null set = []
| otherwise =
["\t<upstream>"]
++ (prettyPrintRemoteId <$> S.toAscList set)
++ ["\t</upstream>"]
-- | Pretty print a single 'RemoteId'.
prettyPrintRemoteId :: RemoteId -> String
prettyPrintRemoteId = \case
RemoteIdHackage p -> pp "hackage" p
RemoteIdBitbucket u r -> pp "bitbucket" $ u ++ "/" ++ r
RemoteIdCodeberg u r -> pp "codeberg" $ u ++ "/" ++ r
RemoteIdFreedesktop u r -> pp "freedesktop-gitlab" $ u ++ "/" ++ r
RemoteIdGentoo p -> pp "gentoo" p
RemoteIdGithub u r -> pp "github" $ u ++ "/" ++ r
RemoteIdGitlab u r -> pp "gitlab" $ u ++ "/" ++ r
RemoteIdGnome u r -> pp "gnome-gitlab" $ u ++ "/" ++ r
RemoteIdLaunchpad p -> pp "launchpad" p
RemoteIdKDE u r -> pp "kde-invent" $ u ++ "/" ++ r
RemoteIdOSDN p -> pp "osdn" p
RemoteIdSavannah p -> pp "savannah" p
RemoteIdSavannahNonGNU p -> pp "savannah-nongnu" p
RemoteIdSourceforge p -> pp "sourceforge" p
RemoteIdSourcehut u r -> pp "sourcehut" $ u ++ "/" ++ r
where
pp t v = "\t\t<remote-id type=\"" ++ t ++ "\">" ++ v ++ "</remote-id>"
-- | Run 'matchURI' on all given strings, collecting the result in a 'S.Set'.
matchURIs :: [String] -> S.Set RemoteId
matchURIs = S.fromList . mapMaybe matchURI
-- | Try to parse the given string using any of the defined URI parsers
matchURI :: String -> Maybe RemoteId
matchURI str = asum $ map runUriP definedParsers
where
runUriP :: URIParser -> Maybe RemoteId
runUriP p = eitherToMaybe $ runUriParser p str
-- @Maybe@ is an Alternative, whereas @Either e@ is not. This is needed
-- to make 'asum' work.
eitherToMaybe :: Either e a -> Maybe a
eitherToMaybe (Left _) = Nothing
eitherToMaybe (Right x) = Just x
-- | All parsers defined in this module
definedParsers :: [URIParser]
definedParsers =
[ hackageParser
, bitbucketParser
, codebergParser
, freedesktopParser
, gentooParser
, githubParser
, gitlabParser
, gnomeParser
, kdeParser
, launchpadParser
, osdnParser
, savannahParser
, savannahNonGnuParser
, sourceforgeParser
, sourcehutParser
]
-- | @'hackage': 'https://hackage.haskell.org/package/{project}'@
hackageParser :: URIParser
hackageParser = URIParser
httpScheme
ignore
(string "hackage.haskell.org")
ignore
(do
(p:_) <- stripPrefixP "/package"
pure p
)
ignore
ignore
(\_ _ _ _ p _ _ -> RemoteIdHackage p)
-- | @"bitbucket": "https://bitbucket.org/{project}"@
bitbucketParser :: URIParser
bitbucketParser = URIParser
(choice [httpScheme, string "git:"])
ignore
(domainOrWWW "bitbucket.org")
ignore
(do
(u:r:_) <- stripPrefixP "/"
(u,) <$> gitPath r
)
ignore
ignore
(\_ _ _ _ (u,r) _ _ -> RemoteIdBitbucket u r)
-- | @"codeberg": "https://codeberg.org/{project}"@
codebergParser :: URIParser
codebergParser = URIParser
(choice [httpScheme, string "git:"])
ignore
(domainOrWWW "codeberg.org")
ignore
(do
(u:r:_) <- stripPrefixP "/"
(u,) <$> gitPath r
)
ignore
ignore
(\_ _ _ _ (u,r) _ _ -> RemoteIdCodeberg u r)
-- | @"freedesktop-gitlab": "https://gitlab.freedesktop.org/{project}.git/"@
freedesktopParser :: URIParser
freedesktopParser = URIParser
(choice [httpScheme, string "git:"])
ignore
(domainOrWWW "gitlab.freedesktop.org")
ignore
(do
(u:r:_) <- stripPrefixP "/"
(u,) <$> gitPath r
)
ignore
ignore
(\_ _ _ _ (u,r) _ _ -> RemoteIdFreedesktop u r)
-- | @'gentoo': 'https://gitweb.gentoo.org/{project}.git/'@
gentooParser :: URIParser
gentooParser = URIParser
httpScheme
ignore
(string "gitweb.gentoo.org")
ignore
(do
(s:_) <- stripPrefixP "/"
gitPath s
)
ignore
ignore
(\_ _ _ _ p _ _ -> RemoteIdGentoo p)
-- | @'github': 'https://github.com/{project}'@
githubParser :: URIParser
githubParser = URIParser
(choice [httpScheme, string "git:"])
ignore
(domainOrWWW "github.com")
ignore
(do
(u:r:_) <- stripPrefixP "/"
(u,) <$> gitPath r
)
ignore
ignore
(\_ _ _ _ (u,r) _ _ -> RemoteIdGithub u r)
-- | @'gitlab': 'https://gitlab.com/{project}'@
gitlabParser :: URIParser
gitlabParser = URIParser
(choice [httpScheme, string "git:"])
ignore
(domainOrWWW "gitlab.com")
ignore
(do
(u:r:_) <- stripPrefixP "/"
(u,) <$> gitPath r
)
ignore
ignore
(\_ _ _ _ (u,r) _ _ -> RemoteIdGitlab u r)
-- | @"gnome-gitlab": "https://gitlab.gnome.org/{project}.git/"@
gnomeParser :: URIParser
gnomeParser = URIParser
(choice [httpScheme, string "git:"])
ignore
(domainOrWWW "gitlab.gnome.org")
ignore
(do
(u:r:_) <- stripPrefixP "/"
(u,) <$> gitPath r
)
ignore
ignore
(\_ _ _ _ (u,r) _ _ -> RemoteIdGnome u r)
-- | @"kde-invent": "https://invent.kde.org/{project}"@
kdeParser :: URIParser
kdeParser = URIParser
(choice [httpScheme, string "git:"])
ignore
(domainOrWWW "invent.kde.org")
ignore
(do
(u:r:_) <- stripPrefixP "/"
(u,) <$> gitPath r
)
ignore
ignore
(\_ _ _ _ (u,r) _ _ -> RemoteIdKDE u r)
-- | @'launchpad': 'https://launchpad.net/{project}'@
launchpadParser :: URIParser
launchpadParser = URIParser
httpScheme
ignore
(domainOrWWW "launchpad.net")
ignore
(do
(p:_) <- stripPrefixP "/"
pure p
)
ignore
ignore
(\_ _ _ _ p _ _ -> RemoteIdLaunchpad p)
-- | @'osdn': 'https://osdn.net/projects/{project}/'@
osdnParser :: URIParser
osdnParser = URIParser
httpScheme
ignore
(domainOrWWW "osdn.net")
ignore
(do
(p:_) <- stripPrefixP "/projects"
pure p
)
ignore
ignore
(\_ _ _ _ p _ _ -> RemoteIdOSDN p)
-- | @"savannah": "https://savannah.gnu.org/projects/{project}"@
savannahParser :: URIParser
savannahParser = URIParser
httpScheme
ignore
(string "savannah.gnu.org")
ignore
(do
(p:_) <- stripPrefixP "/projects"
pure p
)
ignore
ignore
(\_ _ _ _ p _ _ -> RemoteIdSavannah p)
-- | @"savannah-nongnu": "https://savannah.nongnu.org/projects/{project}"@
savannahNonGnuParser :: URIParser
savannahNonGnuParser = URIParser
httpScheme
ignore
(string "savannah.nongnu.org")
ignore
(do
(p:_) <- stripPrefixP "/projects"
pure p
)
ignore
ignore
(\_ _ _ _ p _ _ -> RemoteIdSavannahNonGNU p)
-- | @'sourceforge': 'https://sourceforge.net/projects/{project}/'@
sourceforgeParser :: URIParser
sourceforgeParser = URIParser
httpScheme
ignore
(domainOrWWW "sourceforge.net")
ignore
(do
(p:_) <- stripPrefixP "/projects"
pure p
)
ignore
ignore
(\_ _ _ _ p _ _ -> RemoteIdSourceforge p)
-- | @"sourcehut": "https://sr.ht/{project}/"@
sourcehutParser :: URIParser
sourcehutParser = URIParser
(choice [httpScheme, string "git:"])
ignore
(subdomain "sr.ht")
ignore
(do
(u:r:_) <- stripPrefixP "/"
(u,) <$> gitPath r
)
ignore
ignore
(\_ _ _ _ (u,r) _ _ -> RemoteIdSourcehut u r)
-- | Run a specified 'URIParser' with a string
--
-- Internally, uses 'parseURI' to create a 'URI', and then uses each
-- parser specified in 'URIParser' on a specific part of the uri. These
-- intermediate results are coalesced with the supplied 'mkRemoteId'.
runUriParser
:: URIParser
-> String
-> Either ParseError RemoteId
runUriParser (URIParser {..}) = join . parseIt go
where
go :: Parser (Either ParseError RemoteId)
go = do
cs <- allChars
case parseURI cs of
Just (URI scheme (Just (URIAuth user regname port)) path query fragment) ->
pure $ mkRemoteId
<$> parseIt schemeParser scheme
<*> parseIt userParser user
<*> parseIt regnameParser regname
<*> parseIt portParser port
<*> parseIt pathParser path
<*> parseIt queryParser query
<*> parseIt fragmentParser fragment
_ -> fail $ "Could not parse as a URI: " ++ show cs
parseIt :: Parser a -> String -> Either ParseError a
parseIt p = parse p ""
-- | Convenience function for 'stripPrefix', which uses 'allChars' as the
-- target path. Throws a parse error if 'stripPrefix' fails.
stripPrefixP
:: Path -- ^ The prefix path to strip
-> Parser [String]
stripPrefixP pre = do
targ <- allChars
case stripPrefix pre targ of
Just ps -> pure ps
Nothing -> fail $ "Path prefix does not match: \n"
++ "pre = " ++ show pre ++ "\n"
++ "targ = " ++ show targ ++ "\n"
++ "L.stripPrefix " ++ show (splitDirectories pre)
++ " " ++ show (splitDirectories targ) ++ " = Nothing"
-- | Strips a path of a prefix, then returns the result split along path
-- seperators. Returns 'Nothing' if the prefix path does not match the
-- beginning of the target path.
--
-- Examples:
--
-- >>> stripPrefix "/web/packages" "/web/packages/foo/"
-- Just ["foo"]
--
-- >>> stripPrefix "/" "/foo/bar"
-- Just ["foo","bar"]
--
-- >>> stripPrefix "" "/foo/bar"
-- Just ["/","foo","bar"]
--
-- >>> stripPrefix "/some/thing" "/something/else"
-- Nothing
stripPrefix
:: Path -- ^ The prefix path to strip
-> Path -- ^ The target path to strip from
-> Maybe [String]
stripPrefix pre targ =
L.stripPrefix
(splitDirectories pre)
(splitDirectories targ)
-- | Compares the input stream to the given domain. Parser succeeds if
-- either of the following is true:
--
-- * The input stream matches the target domain exactly
-- * The input stream matches the target domain prepended by @"www."@
--
-- e.g.
--
-- > domainOrWWW "github.com"
--
-- will match on @"github.com"@ or @"www.github.com"@
domainOrWWW
:: Domain
-> Parser ()
domainOrWWW targ = do
sub <- allChars
if sub == ("www." ++ targ) || sub == targ
then pure ()
else fail $ "domainOrWWW did not match:"
++ "\nsub: " ++ show sub
++ "\ntarg: " ++ show targ
-- | Remove any ".git" suffix from the specified string
gitPath
:: String
-> Parser String
gitPath p = case parse go "" p of
Left e -> fail $ show e -- Not ideal, but it works
Right r -> pure r
where
go :: Parser String
go = choice
[ try $ manyTill anyChar (string ".git") <* eof
, allChars
]
-- | Compares the input stream to the given domain. Parser succeeds if the
-- input stream is a subdomain of the target.
subdomain
:: Domain -- ^ Target domain to match against
-> Parser ()
subdomain targ = do
sub <- allChars
if targ `L.isSuffixOf` sub
then pure ()
else fail $ show sub ++ " is not a subdomain of " ++ show targ
ignore :: Parser ()
ignore = pure ()
-- | Matches either of the strings @"http:"@ or @"https:"@
httpScheme :: Parser String
httpScheme = choice
[ try $ string "https:"
, string "http:"
]
-- | Match on every character from the input stream
allChars :: Parser String
allChars = many anyChar