panbuild 0.0.11

The universal builder.
Documentation
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
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::manifests::manifest::{AbstractManifest, AbstractModule, Priority};

const CONTROL_FILE_SEPARATOR: &str = ":";

pub const ALLOWED_SECTIONS: [&'static str; 57] = [
    "admin",
    "cli-mono",
    "comm",
    "database",
    "debug",
    "devel",
    "doc",
    "editors",
    "education",
    "electronics",
    "embedded",
    "fonts",
    "games",
    "gnome",
    "gnu-r",
    "gnustep",
    "graphics",
    "hamradio",
    "haskell",
    "httpd",
    "interpreters",
    "introspection",
    "java",
    "javascript",
    "kde",
    "kernel",
    "libdevel",
    "libs",
    "lisp",
    "localization",
    "mail",
    "math",
    "metapackages",
    "misc",
    "net",
    "news",
    "ocaml",
    "oldlibs",
    "otherosfs",
    "perl",
    "php",
    "python",
    "ruby",
    "rust",
    "science",
    "shells",
    "sound",
    "tasks",
    "tex",
    "text",
    "utils",
    "vcs",
    "video",
    "web",
    "x11",
    "xfce",
    "zope",
];

// list of packages (see package fields below).
const PACKAGES: &str = "packages";

// **** Package fields
const PACKAGE_NAME: &str = "Package";
// A multi-line string
const DESCRIPTION: &str = "Description";
// Can be "any"
const ARCHITECTURE: &str = "Architecture";
const MULTI_ARCH: &str = "Multi-Arch";
// These seven fields are used to declare a dependency relationship by one package on another.
// See https://www.debian.org/doc/debian-policy/ch-relationships.html#binary-dependencies-depends-recommends-suggests-enhances-pre-depends
const DEPENDS: &str = "Depends";
const RECOMMENDS: &str = "Recommends";
const SUGGESTS: &str = "Suggests";
const ENHANCES: &str = "Enhances";
const PRE_DEPENDS: &str = "Pre-Depends";
const CONFLICTS: &str = "Conflicts";
const BREAKS: &str = "Breaks";

#[derive(Serialize, Deserialize, Default, Debug)]
pub struct DebianManifest {
    // The name of the source described in this manifest.
    // (mandatory)
    pub source: String,
    pub version: String,
    // The packages in the archive areas main, contrib and non-free are grouped
    // further into sections to simplify handling.
    pub section: String,
    pub priority: String,
    // Format is name <email@address.com>
    // (mandatory)
    pub maintainer: String,
    // List of the names and email addresses of co-maintainers of the package, if any.
    // Format is name <email@address.com>
    pub uploaders: Vec<String>,
    pub build_depends: Vec<String>,
    // A semver reference to a "canonical" version.
    // (mandatory)
    pub standards_version: String,
    // URL of a website to browser the source code.
    pub homepage: String,
    // URL of a website to browser the source code.
    pub vcs_browser: String,
    // URL of the git repo.
    pub vcs_git: String,
}

fn parse_paragraphs(content: &str, paragraphs: &mut Vec<String>) {
    let content_str = content.to_string();
    let lines = content_str.split("\n");
    let mut paragraph: String = String::from("");

    for line in lines {
        if !is_empty_line(line) {
            paragraph.push_str(line);
            paragraph.push_str("\n");
        }
        if is_empty_line(line) && !paragraph.is_empty() {
            paragraphs.push(paragraph);
            paragraph = String::from("");
        }
    }
}

fn is_empty_line(line: &str) -> bool {
    for c in line.chars() {
        if c == ' ' {
            continue;
        }
        if c == '\t' {
            continue;
        }
        return false;
    }
    return true;
}

fn is_indented_line(line: &str) -> bool {
    for c in line.chars() {
        if c == ' ' {
            return true;
        }
        if c == '\t' {
            return true;
        }
        return false;
    }
    return false;
}

fn is_commented_line(line: &str) -> bool {
    for c in line.chars() {
        if c == ' ' {
            continue;
        }
        if c == '\t' {
            continue;
        }
        if c == '#' {
            return true;
        }
        return false;
    }
    return false;
}

pub fn parse(ctx: &mut crate::execution_context::ExecutionContext) {
    let mut paragraphs: Vec<String> = vec![];
    parse_paragraphs(&ctx.content, &mut paragraphs);

    ctx.manifest = crate::manifests::manifest::AbstractManifest::default();
    if paragraphs.len() < 2 {
        panic!("There is only {} paragraphs in the debian control file?", paragraphs.len())
    }

    let mut debian_manifest = DebianManifest::default();

    let first_paragraph = &paragraphs[0];
    for line in first_paragraph.split('\n') {
        if line.starts_with(" ") {
            // Obviously a mistake
            continue;
        }
        if is_empty_line(line) {
            continue;
        }
        if is_commented_line(line) {
            continue;
        }
        let parts: Vec<&str> = line.split(CONTROL_FILE_SEPARATOR).collect();
        if parts.len() < 2 {
            // FIXME we should return a Result<> instead of exiting here or
            // returning a default value.
            eprintln!("Invalid debian control file line {}", line);
            return;
        }
        let field_name = parts[0].trim();

        let value_parts: Vec<&str> = parts[1..].to_vec();
        let mut field_value = value_parts.join(CONTROL_FILE_SEPARATOR);
        field_value = field_value.trim().to_string();

        if field_name == "Source" {
            debian_manifest.source = field_value.to_string();
        }
        if field_name == "Maintainer" {
            debian_manifest.maintainer = field_value.to_string();
        }
        if field_name == "Version" {
            debian_manifest.version = field_value.to_string();
        }
        if field_name == "Priority" {
            debian_manifest.priority = field_value.to_string();
        }
        if field_name == "Standards-Version" {
            debian_manifest.standards_version = field_value.to_string();
        }
        if field_name == "Homepage" {
            debian_manifest.vcs_browser = field_value.to_string();
            debian_manifest.homepage = field_value.to_string();
        }
        if field_name == "Section" {
            debian_manifest.section = field_value.to_string();
            if !ALLOWED_SECTIONS.contains(&&debian_manifest.section[..]) {
                // FIXME we should return a Result<> instead of exiting here or
                // returning a default value.
                eprintln!("Invalid debian control section {}", debian_manifest.section);
                return;
            }
        }
    }

    for paragraph_index in 1..paragraphs.len() {
        let mut package = AbstractModule::default();
        let paragraph = &paragraphs[paragraph_index];
        let mut last_field_name: String = String::from("");

        // Those fields can be multi-line.
        let mut build_depends: String = String::from("");
        let mut description: String = String::from("");

        for line in paragraph.split('\n') {
            if is_commented_line(line) {
                continue;
            }
            if is_empty_line(line) {
                continue;
            }

            let mut field_name: String;
            let mut field_value: String;

            if is_indented_line(line) {
                field_name = last_field_name.clone();
                field_value = line.to_string();
            } else {
                let parts: Vec<&str> = line.split(CONTROL_FILE_SEPARATOR).collect();
                if parts.len() < 2 {
                    // FIXME we should return a Result<> instead of exiting here or
                    // returning a default value.
                    eprintln!("Invalid debian control file line {}", line);
                    return;
                }

                field_name = parts[0].trim().to_string();
                field_value = String::from("");
                for part in parts {
                    if part == field_name {
                        continue;
                    }
                    if !field_value.is_empty() {
                        field_value.push_str(CONTROL_FILE_SEPARATOR);
                    }
                    field_value.push_str(part);
                }
                last_field_name = field_name.clone();
            }

            if field_name == PACKAGE_NAME {
                package.name = field_value.trim().to_string();
            } else if field_name == ARCHITECTURE {
            } else if field_name == DESCRIPTION {
                // Here we append because the field can be multi-line.
                description.push_str(&field_value);
            } else if field_name == DEPENDS {
                build_depends.push_str(&field_value);
            } else {
                eprintln!("Unknown debian package field {}", field_name);
            }
        }

        for package_name in build_depends.split(",") {
            if package_name.is_empty() {
                continue;
            }
            let mut new_module = crate::manifests::manifest::AbstractModule::default();

            let module_spec = package_name.trim().to_string();
            // Still not sure what this is about.
            if module_spec.starts_with("${") {
                continue;
            }

            let mut module_spec_parts: Vec<&str> = module_spec.split(" ").collect();

            new_module.name = module_spec_parts[0].to_string();
            module_spec_parts.remove(0);

            let mut version_spec = String::from("");
            for part in module_spec_parts {
                if !version_spec.is_empty() {
                    version_spec.push_str(" ");
                }
                version_spec.push_str(part);
            }
            new_module.version = version_spec;

            package.depends_on.push(new_module);
        }
        ctx.manifest.depends_on.push(package);
    }

    eprintln!("finished parsing debian control file.");
    ctx.manifest.debian_manifest = Some(debian_manifest);
}

pub fn file_path_matches(path: &str) -> bool {
    // FIXME should we add the debian/ part to the path too?
    if path.ends_with("control") {
        return true;
    }
    return false;
}

pub fn file_content_matches(content: &str) -> bool {
    return false;
}

const DEBIAN_CONTROL_EXAMPLE: &str = r###"
Source: package_name
Section: x11
Priority: optional
Maintainer: me <me@cloud.com>
Build-Depends:
 debhelper (>= 12),
 gtk-doc-tools,
 libsecret-1-dev,
 libfeedback-dev,
 libgnome-desktop-3-dev,
 libhandy-0.0-dev (>= 0.0.12),
 libpam0g-dev,
# to run the tests
 at-spi2-core,
 gnome-themes-extra-data,
 phoc,
 xauth,
Standards-Version: 3.2.2
Homepage: https://code.cloud.com/projects/package_name

Package: other_package_name
Architecture: any
Depends:
 ${misc:Depends},
 ${shlibs:Depends},
 fonts-lato,
 gsettings-desktop-schemas,
 phoc (>= 0.4.0),
Recommends:
 feedbackd,
 iio-sensor-proxy,
 gnome-session,
 phoc,
Provides:
 notification-daemon,
 polkit-1-auth-agent,
Description: Here's a description of the sub-package
 on multiple lines.

"###;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    pub fn test_is_empty_line() {
        assert_eq!(true, is_empty_line(""));
        assert_eq!(true, is_empty_line("\t\t"));
        assert_eq!(true, is_empty_line("                   "));
        assert_eq!(false, is_empty_line("                   word"));
    }

    #[test]
    pub fn test_is_commented_line() {
        assert_eq!(false, is_commented_line(""));
        assert_eq!(true, is_commented_line("# comment"));
        assert_eq!(true, is_commented_line("         # comment"));
        assert_eq!(false, is_commented_line("Field: Value # with a comment after."));
    }

    #[test]
    pub fn test_is_indented_line() {
        assert_eq!(false, is_indented_line(""));
        assert_eq!(true, is_indented_line("  line"));
        assert_eq!(true, is_indented_line("  comment    "));
        assert_eq!(false, is_indented_line("Field: Value # with a comment after."));
    }

    #[test]
    pub fn test_parse() {
        let mut ctx = crate::execution_context::ExecutionContext::default();
        ctx.content = DEBIAN_CONTROL_EXAMPLE.to_string();
        parse(&mut ctx);
        match ctx.manifest.debian_manifest {
            None => panic!("Error while parsing the debian manifest."),
            Some(manifest) => {
                assert!(manifest.source == "package_name", "The app name was not package_name!",);
                assert!(manifest.vcs_browser == "https://code.cloud.com/projects/package_name");
            }
        }
    }

    #[test]
    pub fn test_file_path_matches() {
        assert!(file_path_matches("control"));
        assert!(file_path_matches("/path/to/control"));
        assert!(!file_path_matches("/path/to/file.yaml"));
        assert!(!file_path_matches("/path/to/file.json"));
        assert!(!file_path_matches(""));
        assert!(!file_path_matches("/////////////"));
    }
}