forked from backstage/mkdocs-monorepo-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerger.py
80 lines (65 loc) · 2.78 KB
/
merger.py
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
# Copyright 2019 Spotify AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from tempfile import TemporaryDirectory
from distutils.dir_util import copy_tree
import logging
import os
from os import listdir
from os.path import isfile, join
from mkdocs.utils import warning_filter
log = logging.getLogger(__name__)
log.addFilter(warning_filter)
# This collects the multiple docs/ folders and merges them together.
class Merger:
def __init__(self, config):
self.config = config
self.root_docs_dir = config['docs_dir']
self.docs_dirs = list()
self.append('', self.root_docs_dir)
self.files_source_dir = dict()
def append(self, alias, docs_dir):
self.docs_dirs.append([alias, docs_dir])
def merge(self):
self.temp_docs_dir = TemporaryDirectory('', 'docs_')
aliases = list(filter(lambda docs_dir: len(docs_dir) > 0, map(
lambda docs_dir: docs_dir[0], self.docs_dirs)))
if len(aliases) != len(set(aliases)):
log.critical(
"[mkdocs-monorepo] You cannot have duplicated site names. " +
"Current registered site names in the monorepository: {}".format(', '.join(aliases)))
raise SystemExit(1)
for alias, docs_dir in self.docs_dirs:
if len(alias) == 0:
source_dir = docs_dir
dest_dir = self.temp_docs_dir.name
else:
source_dir = "{}/docs".format(docs_dir)
dest_dir = "{}/{}".format(self.temp_docs_dir.name, alias)
if os.path.exists(source_dir):
copy_tree(source_dir, dest_dir)
files = [f for f in listdir(source_dir) if isfile(join(source_dir, f))]
for f in files:
src = join(source_dir, f)
dest = join(dest_dir, f)
self.files_source_dir[dest] = src
else:
log.critical(
"[mkdocs-monorepo] The {} path is not valid. ".format(source_dir) +
"Please update your 'nav' with a valid path.")
raise SystemExit(1)
return str(self.temp_docs_dir.name)
def getFilesSourceFolder(self):
return self.files_source_dir
def cleanup(self):
return self.temp_docs_dir.cleanup()