{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
module Distribution.License (
License(..),
knownLicenses,
licenseToSPDX,
licenseFromSPDX,
) where
import Distribution.Compat.Prelude
import Prelude ()
import Distribution.Parsec
import Distribution.Pretty
import Distribution.Version
import qualified Distribution.Compat.CharParsing as P
import qualified Data.Map.Strict as Map
import qualified Distribution.SPDX as SPDX
import qualified Text.PrettyPrint as Disp
data License =
GPL (Maybe Version)
| AGPL (Maybe Version)
| LGPL (Maybe Version)
| BSD2
| BSD3
| BSD4
| MIT
| ISC
| MPL Version
| Apache (Maybe Version)
| PublicDomain
| AllRightsReserved
| UnspecifiedLicense
| OtherLicense
| UnknownLicense String
deriving (forall x. Rep License x -> License
forall x. License -> Rep License x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep License x -> License
$cfrom :: forall x. License -> Rep License x
Generic, ReadPrec [License]
ReadPrec License
Int -> ReadS License
ReadS [License]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [License]
$creadListPrec :: ReadPrec [License]
readPrec :: ReadPrec License
$creadPrec :: ReadPrec License
readList :: ReadS [License]
$creadList :: ReadS [License]
readsPrec :: Int -> ReadS License
$creadsPrec :: Int -> ReadS License
Read, Int -> License -> ShowS
[License] -> ShowS
License -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [License] -> ShowS
$cshowList :: [License] -> ShowS
show :: License -> String
$cshow :: License -> String
showsPrec :: Int -> License -> ShowS
$cshowsPrec :: Int -> License -> ShowS
Show, License -> License -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: License -> License -> Bool
$c/= :: License -> License -> Bool
== :: License -> License -> Bool
$c== :: License -> License -> Bool
Eq, Typeable, Typeable License
License -> DataType
License -> Constr
(forall b. Data b => b -> b) -> License -> License
forall a.
Typeable a
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> License -> u
forall u. (forall d. Data d => d -> u) -> License -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> License -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> License -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> License -> m License
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> License -> m License
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c License
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> License -> c License
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c License)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c License)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> License -> m License
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> License -> m License
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> License -> m License
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> License -> m License
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> License -> m License
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> License -> m License
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> License -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> License -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> License -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> License -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> License -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> License -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> License -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> License -> r
gmapT :: (forall b. Data b => b -> b) -> License -> License
$cgmapT :: (forall b. Data b => b -> b) -> License -> License
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c License)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c License)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c License)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c License)
dataTypeOf :: License -> DataType
$cdataTypeOf :: License -> DataType
toConstr :: License -> Constr
$ctoConstr :: License -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c License
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c License
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> License -> c License
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> License -> c License
Data)
instance Binary License
instance Structured License
instance NFData License where rnf :: License -> ()
rnf = forall a. (Generic a, GNFData (Rep a)) => a -> ()
genericRnf
knownLicenses :: [License]
knownLicenses :: [License]
knownLicenses = [ Maybe Version -> License
GPL forall {a}. Maybe a
unversioned, Maybe Version -> License
GPL ([Int] -> Maybe Version
version [Int
2]), Maybe Version -> License
GPL ([Int] -> Maybe Version
version [Int
3])
, Maybe Version -> License
LGPL forall {a}. Maybe a
unversioned, Maybe Version -> License
LGPL ([Int] -> Maybe Version
version [Int
2, Int
1]), Maybe Version -> License
LGPL ([Int] -> Maybe Version
version [Int
3])
, Maybe Version -> License
AGPL forall {a}. Maybe a
unversioned, Maybe Version -> License
AGPL ([Int] -> Maybe Version
version [Int
3])
, License
BSD2, License
BSD3, License
MIT, License
ISC
, Version -> License
MPL ([Int] -> Version
mkVersion [Int
2, Int
0])
, Maybe Version -> License
Apache forall {a}. Maybe a
unversioned, Maybe Version -> License
Apache ([Int] -> Maybe Version
version [Int
2, Int
0])
, License
PublicDomain, License
AllRightsReserved, License
OtherLicense]
where
unversioned :: Maybe a
unversioned = forall {a}. Maybe a
Nothing
version :: [Int] -> Maybe Version
version = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> Version
mkVersion
licenseToSPDX :: License -> SPDX.License
licenseToSPDX :: License -> License
licenseToSPDX License
l = case License
l of
GPL Maybe Version
v | Maybe Version
v forall a. Eq a => a -> a -> Bool
== [Int] -> Maybe Version
version [Int
2] -> LicenseId -> License
spdx LicenseId
SPDX.GPL_2_0_only
GPL Maybe Version
v | Maybe Version
v forall a. Eq a => a -> a -> Bool
== [Int] -> Maybe Version
version [Int
3] -> LicenseId -> License
spdx LicenseId
SPDX.GPL_3_0_only
LGPL Maybe Version
v | Maybe Version
v forall a. Eq a => a -> a -> Bool
== [Int] -> Maybe Version
version [Int
2,Int
1] -> LicenseId -> License
spdx LicenseId
SPDX.LGPL_2_1_only
LGPL Maybe Version
v | Maybe Version
v forall a. Eq a => a -> a -> Bool
== [Int] -> Maybe Version
version [Int
3] -> LicenseId -> License
spdx LicenseId
SPDX.LGPL_3_0_only
AGPL Maybe Version
v | Maybe Version
v forall a. Eq a => a -> a -> Bool
== [Int] -> Maybe Version
version [Int
3] -> LicenseId -> License
spdx LicenseId
SPDX.AGPL_3_0_only
License
BSD2 -> LicenseId -> License
spdx LicenseId
SPDX.BSD_2_Clause
License
BSD3 -> LicenseId -> License
spdx LicenseId
SPDX.BSD_3_Clause
License
BSD4 -> LicenseId -> License
spdx LicenseId
SPDX.BSD_4_Clause
License
MIT -> LicenseId -> License
spdx LicenseId
SPDX.MIT
License
ISC -> LicenseId -> License
spdx LicenseId
SPDX.ISC
MPL Version
v | Version
v forall a. Eq a => a -> a -> Bool
== [Int] -> Version
mkVersion [Int
2,Int
0] -> LicenseId -> License
spdx LicenseId
SPDX.MPL_2_0
Apache Maybe Version
v | Maybe Version
v forall a. Eq a => a -> a -> Bool
== [Int] -> Maybe Version
version [Int
2,Int
0] -> LicenseId -> License
spdx LicenseId
SPDX.Apache_2_0
License
AllRightsReserved -> License
SPDX.NONE
License
UnspecifiedLicense -> License
SPDX.NONE
License
OtherLicense -> LicenseRef -> License
ref (Maybe String -> String -> LicenseRef
SPDX.mkLicenseRef' forall {a}. Maybe a
Nothing String
"OtherLicense")
License
PublicDomain -> LicenseRef -> License
ref (Maybe String -> String -> LicenseRef
SPDX.mkLicenseRef' forall {a}. Maybe a
Nothing String
"PublicDomain")
UnknownLicense String
str -> LicenseRef -> License
ref (Maybe String -> String -> LicenseRef
SPDX.mkLicenseRef' forall {a}. Maybe a
Nothing String
str)
License
_ -> LicenseRef -> License
ref (Maybe String -> String -> LicenseRef
SPDX.mkLicenseRef' forall {a}. Maybe a
Nothing forall a b. (a -> b) -> a -> b
$ forall a. Pretty a => a -> String
prettyShow License
l)
where
version :: [Int] -> Maybe Version
version = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> Version
mkVersion
spdx :: LicenseId -> License
spdx = LicenseExpression -> License
SPDX.License forall b c a. (b -> c) -> (a -> b) -> a -> c
. LicenseId -> LicenseExpression
SPDX.simpleLicenseExpression
ref :: LicenseRef -> License
ref LicenseRef
r = LicenseExpression -> License
SPDX.License forall a b. (a -> b) -> a -> b
$ SimpleLicenseExpression
-> Maybe LicenseExceptionId -> LicenseExpression
SPDX.ELicense (LicenseRef -> SimpleLicenseExpression
SPDX.ELicenseRef LicenseRef
r) forall {a}. Maybe a
Nothing
licenseFromSPDX :: SPDX.License -> License
licenseFromSPDX :: License -> License
licenseFromSPDX License
SPDX.NONE = License
AllRightsReserved
licenseFromSPDX License
l =
forall a. a -> Maybe a -> a
fromMaybe (String -> License
mungle forall a b. (a -> b) -> a -> b
$ forall a. Pretty a => a -> String
prettyShow License
l) forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup License
l Map License License
m
where
m :: Map.Map SPDX.License License
m :: Map License License
m = forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
filter (License -> Bool
isSimple forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst ) forall a b. (a -> b) -> a -> b
$
forall a b. (a -> b) -> [a] -> [b]
map (\License
x -> (License -> License
licenseToSPDX License
x, License
x)) [License]
knownLicenses
isSimple :: License -> Bool
isSimple (SPDX.License (SPDX.ELicense (SPDX.ELicenseId LicenseId
_) Maybe LicenseExceptionId
Nothing)) = Bool
True
isSimple License
_ = Bool
False
mungle :: String -> License
mungle String
name = forall a. a -> Maybe a -> a
fromMaybe (String -> License
UnknownLicense (forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Char -> Maybe Char
mangle String
name)) (forall a. Parsec a => String -> Maybe a
simpleParsec String
name)
mangle :: Char -> Maybe Char
mangle Char
c
| Char -> Bool
isAlphaNum Char
c = forall a. a -> Maybe a
Just Char
c
| Bool
otherwise = forall {a}. Maybe a
Nothing
instance Pretty License where
pretty :: License -> Doc
pretty (GPL Maybe Version
version) = String -> Doc
Disp.text String
"GPL" Doc -> Doc -> Doc
<<>> Maybe Version -> Doc
dispOptVersion Maybe Version
version
pretty (LGPL Maybe Version
version) = String -> Doc
Disp.text String
"LGPL" Doc -> Doc -> Doc
<<>> Maybe Version -> Doc
dispOptVersion Maybe Version
version
pretty (AGPL Maybe Version
version) = String -> Doc
Disp.text String
"AGPL" Doc -> Doc -> Doc
<<>> Maybe Version -> Doc
dispOptVersion Maybe Version
version
pretty (MPL Version
version) = String -> Doc
Disp.text String
"MPL" Doc -> Doc -> Doc
<<>> Version -> Doc
dispVersion Version
version
pretty (Apache Maybe Version
version) = String -> Doc
Disp.text String
"Apache" Doc -> Doc -> Doc
<<>> Maybe Version -> Doc
dispOptVersion Maybe Version
version
pretty (UnknownLicense String
other) = String -> Doc
Disp.text String
other
pretty License
other = String -> Doc
Disp.text (forall a. Show a => a -> String
show License
other)
instance Parsec License where
parsec :: forall (m :: * -> *). CabalParsing m => m License
parsec = do
String
name <- forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m String
P.munch1 Char -> Bool
isAlphaNum
Maybe Version
version <- forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
P.optional (forall (m :: * -> *). CharParsing m => Char -> m Char
P.char Char
'-' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall a (m :: * -> *). (Parsec a, CabalParsing m) => m a
parsec)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! case (String
name, Maybe Version
version :: Maybe Version) of
(String
"GPL", Maybe Version
_ ) -> Maybe Version -> License
GPL Maybe Version
version
(String
"LGPL", Maybe Version
_ ) -> Maybe Version -> License
LGPL Maybe Version
version
(String
"AGPL", Maybe Version
_ ) -> Maybe Version -> License
AGPL Maybe Version
version
(String
"BSD2", Maybe Version
Nothing) -> License
BSD2
(String
"BSD3", Maybe Version
Nothing) -> License
BSD3
(String
"BSD4", Maybe Version
Nothing) -> License
BSD4
(String
"ISC", Maybe Version
Nothing) -> License
ISC
(String
"MIT", Maybe Version
Nothing) -> License
MIT
(String
"MPL", Just Version
version') -> Version -> License
MPL Version
version'
(String
"Apache", Maybe Version
_ ) -> Maybe Version -> License
Apache Maybe Version
version
(String
"PublicDomain", Maybe Version
Nothing) -> License
PublicDomain
(String
"AllRightsReserved", Maybe Version
Nothing) -> License
AllRightsReserved
(String
"OtherLicense", Maybe Version
Nothing) -> License
OtherLicense
(String, Maybe Version)
_ -> String -> License
UnknownLicense forall a b. (a -> b) -> a -> b
$ String
name forall a. [a] -> [a] -> [a]
++
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" ((Char
'-'forall a. a -> [a] -> [a]
:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Pretty a => a -> String
prettyShow) Maybe Version
version
dispOptVersion :: Maybe Version -> Disp.Doc
dispOptVersion :: Maybe Version -> Doc
dispOptVersion Maybe Version
Nothing = Doc
Disp.empty
dispOptVersion (Just Version
v) = Version -> Doc
dispVersion Version
v
dispVersion :: Version -> Disp.Doc
dispVersion :: Version -> Doc
dispVersion Version
v = Char -> Doc
Disp.char Char
'-' Doc -> Doc -> Doc
<<>> forall a. Pretty a => a -> Doc
pretty Version
v